C++/ AVR. The new[](unsignet int) operator is not defined

I write a program for ATmega328P in C++. When compiling, an error occurs - undefined reference to operator new[](unsigned int) swears at the line uint8_t* arr = new uint8_t[10];

I use gcc, g++ , and everything else from Atmel Studio Settings for the C11 and C++14 language standards, respectively.

What should I do? How to treat it?

Environment Messages:

====================[ Build | all | Debug ]=====================================
"D:\Programs\CLion 2019.1.3\bin\cmake\win\bin\cmake.exe" --build D:\Projects\Tests\ModBusRTU\cmake-build-debug --target all -- -j 4
Scanning dependencies of target ControllerTest-atmega328p.elf
[ 10%] Building CXX object CMakeFiles/ControllerTest-atmega328p.elf.dir/src/ModBusRTU/MBBuffer.cpp.obj
[ 20%] Building CXX object CMakeFiles/ControllerTest-atmega328p.elf.dir/src/ModBusRTU/MBMemoryStorage.cpp.obj
[ 30%] Building CXX object CMakeFiles/ControllerTest-atmega328p.elf.dir/src/ModBusRTU/MbFlags.cpp.obj
[ 40%] Building CXX object CMakeFiles/ControllerTest-atmega328p.elf.dir/src/ModBusRTU/ModbusMemoryBlock.cpp.obj
[ 50%] Building CXX object CMakeFiles/ControllerTest-atmega328p.elf.dir/src/ModBusRTU/ModbusRTU.cpp.obj
[ 60%] Building CXX object CMakeFiles/ControllerTest-atmega328p.elf.dir/src/main.cpp.obj
[ 70%] Linking CXX executable ControllerTest-atmega328p.elf
CMakeFiles/ControllerTest-atmega328p.elf.dir/src/ModBusRTU/MBBuffer.cpp.obj: In function `ModBus::MBBuffer::MBBuffer(int)':
D:\Projects\Tests\ModBusRTU\src\ModBusRTU/MBBuffer.cpp:9: undefined reference to `operator new[](unsigned int)'
CMakeFiles/ControllerTest-atmega328p.elf.dir/src/ModBusRTU/ModbusRTU.cpp.obj: In function `ModBus::ModbusRTU::Init(unsigned char, ModBus::BaudRate, unsigned char, ModBus::MbFlags)':
D:\Projects\Tests\ModBusRTU\src\ModBusRTU/ModbusRTU.cpp:18: undefined reference to `operator new(unsigned int)'
CMakeFiles/ControllerTest-atmega328p.elf.dir/src/main.cpp.obj: In function `setup()':
D:\Projects\Tests\ModBusRTU\src/main.cpp:9: undefined reference to `operator new(unsigned int)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [ControllerTest-atmega328p.elf] Error 1
CMakeFiles\ControllerTest-atmega328p.elf.dir\build.make:158: recipe for target 'ControllerTest-atmega328p.elf' failed
mingw32-make.exe[1]: *** [CMakeFiles/ControllerTest-atmega328p.elf.dir/all] Error 2
CMakeFiles\Makefile2:108: recipe for target 'CMakeFiles/ControllerTest-atmega328p.elf.dir/all' failed
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed
 1
Author: GoodSimon, 2019-06-05

1 answers

In c++, no operators are defined for AVR microcontrollers new, new[], delete and delete[] it follows that you need to define them yourself. Here is an example of how to do this the easiest way. Not sure if my implementation of delete[] is correct.

For myself, I created a separate Global. h and Global.cpp with the following content:

Global.h

#ifndef CONTROLLERTEST_GLOBAL_H
#define CONTROLLERTEST_GLOBAL_H    
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>    
void* operator new(size_t size);    
void* operator new[](size_t size);    
void operator delete(void* ptr);
void operator delete[](void* ptr);    
__extension__ typedef int __guard __attribute__((mode (__DI__)));    
extern "C" int __cxa_guard_acquire(__guard*);
extern "C" void __cxa_guard_release(__guard*);
extern "C" void __cxa_guard_abort(__guard*);    
extern "C" void __cxa_pure_virtual(void);
#endif //CONTROLLERTEST_GLOBAL_H

Global.cpp

#include "Global.h"   
void* operator new(size_t size) { return malloc(size); }    
void* operator new[](size_t size) { return malloc(size); }    
void operator delete(void* ptr) { free(ptr); }    
void operator delete[](void* ptr) { free(ptr); }    
int __cxa_guard_acquire(__guard* g) { return !*(char*) (g); }    
void __cxa_guard_release(__guard* g) { *(char*) g = 1; }    
void __cxa_guard_abort(__guard*) {}    
void __cxa_pure_virtual(void) {}
 0
Author: GoodSimon, 2019-06-05 11:19:24