简体   繁体   中英

How to use STL on ARM Cortex-M chips?

I am using STM32 dev board with MCU G++ Compiler & Linker: arm-none-eabi-g++ . However, it seems not compatible with STL:

#include <list>

int main (void)
{
    std::list<int> list;

    list.push_back(1);
    list.sort();

    return 0;
}

The linker error messages:

abort.c:(.text.abort+0xa): undefined reference to `_exit'
fstatr.c:(.text._fstat_r+0x10): undefined reference to `_fstat'
signalr.c:(.text._kill_r+0x10): undefined reference to `_kill'
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
writer.c:(.text._write_r+0x12): undefined reference to `_write'
closer.c:(.text._close_r+0xc): undefined reference to `_close'
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
lseekr.c:(.text._lseek_r+0x12): undefined reference to `_lseek'
readr.c:(.text._read_r+0x12): undefined reference to `_read'

The C++ STL seems dependent on operating systems. Since the micro-controller has no such things, those essential parts are missing while linking ELF.

The problem is how can I use STL on STM32 L4-series chips?

This problem can be solved by adding -specs=nosys.specs to the G++ linker. This links in a separate library with implementations for all required system functions.

See also: http://pabigot.github.io/bspacm/newlib.html

The C Standardlibrary needs some basic functions (called stubs) to work properly. Normally the OS provides these functions.

-specs= nosys.specs provides very dumb versions of these functions. In your compiler path under /share/doc/gcc-arm-none-eabi/pdf there should be a pdf libc.pdf there you will find some information how to implement those stubs yourself (chapter Systemcalls).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM