Compiling Error when include <memory>

Not sure if this problem has been reported. I wrote a simple class,

File.h

#ifndef FS_H
#define FS_H
#undef min
#undef max
#include <memory>
class File
{
public:
    File() {
        _info = 0;
    }
    int getInfo(void);
protected:
    int _info;
};
#endif

and File.cpp

#include "File.h"

int File::getInfo() {
  return _info;
}

The Arduino ino file,

#include "File.h"

File f;
void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

I got tons of compile errors (too many lines, I just copied some lines from Arduino console)

/home/ubuntu/.arduino15/packages/realtek/tools/ameba_d_asdk_toolchain/1.0.1/arm-none-eabi/include/c++/6.5.0/bits/basic_string.h: In function 'std::__cxx11::string std::__cxx11::to_string(long double)':
/home/ubuntu/.arduino15/packages/realtek/tools/ameba_d_asdk_toolchain/1.0.1/arm-none-eabi/include/c++/6.5.0/bits/basic_string.h:5538:45: error: '_rtl_vsnprintf' is not a member of 'std'
     return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
                                             ^~~


from /home/ubuntu/.arduino15/packages/realtek/hardware/AmebaD/3.1.2/libraries/OOP/src/File.h:7,
from /home/ubuntu/.arduino15/packages/realtek/hardware/AmebaD/3.1.2/libraries/OOP/src/File.cpp:1:
/home/ubuntu/.arduino15/packages/realtek/hardware/AmebaD/3.1.2/system/component/soc/realtek/amebad/fwlib/include/rtl8721d_ram_libc.h:18:12: note:   '_rtl_vsnprintf'
 extern int _rtl_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap);
            ^~~~~~~~~~~~~~
exit status 1

The ino file shows below compiles without any problem!!

#undef min
#undef max
#include <memory>

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

Environment:Ubuntu 20.04, Arduino 1.8.19. Ameba SDK 3.1.2
Two boards are installed in the Board Manager, Arduino AVR Board and AmebaD ARM (32-bits) Boards.

I have also modified Arduino.h and un-comment the 28th line so the line looks like,

#define Arduino_STD_PRINTF

Any suggestion/comment ?

1 Like

@mstsai

To speed up code and save memory space, some of the GCC standard functions (printf, sprintf …) are replaced with speed optimized ROM code through #defines, which can be identified by the _rtl_ prefix. These defines are probably causing conflicts when code in <memory> calls GCC standard functions. Refer to AN0400 Application Note chapter 5 for details on which functions are replaced and instructions on how to choose to compile in GCC standard library functions.

is there a specific reason you are including <memory>?

To compile in the GCC standard library, #define Arduino_STD_PRINTF by itself is not sufficient. At minimum you will also need to modify platform_stdlib_rtl8721d.h. I have tested with this and it compiles fine.

Thanks for the response. I need to use smart pointer which is defined in , I think. I also need to include for function pointer, although I think I can use traditional function pointer instead.
For modification of platform_stdlib_rtl8721d.h, could you please be more specific? I checked the file. The locations which are related to the errors I got are

#ifndef STD_PRINTF
        #define printf                                          _rtl_printf
        #define sprintf                                         _rtl_sprintf
        #define snprintf                                        _rtl_snprintf                   // NULL function
        #define vsnprintf                                       _rtl_vsnprintf
        #define sscanf                                          _rtl_sscanf     //if use sscanf in std libc.a, please delete _strtol_r symbol in rlx8721d_rom_symbol_acut.ld
#endif

What do I suppose to do for these statements?

Instead of modifying the codes according to AN0400 , I comment the whole section between #ifndef STD_PRINTF and #endif. Now, it compiles without error. So, I am using the standard library rather than the optimized codes for Ameba, right?

yes, without the defines, the functions will not be replaced.

1 Like

An easier solution
Uncomment
#define STD_PRINTF
at line 5 of /platform_stdlib_rtl8721d.h.

1 Like