cancel
Showing results for 
Search instead for 
Did you mean: 

GCC -flto causes linker errors for STM32L476RGT6

Louis Cloete work
Associate III

I have a program for which I would like to enable link-time optimization. However, once I pass the -flto flag to the compiler and linker, I get two linker errors, one complaining about multiple definition of _exit and undefined reference to _sbrk. I found various places online about people having issues with undefined references in conjunction with lto, but not redefinition errors. Does anybody have any idea what I can do here?

PS lto is a nice-to-have for this project. I can live without it, but it would be nice if I can get it to work.

1 REPLY 1
berendi
Principal

Looking at the full error message, _exit() is apparently defined in libnosys.a too, besides syscalls.c. Both libnosys.a and syscalls.c serve the same function, providing stubs for nonexistent operating system calls. I have no idea why are both of them present in the standard STM32 project. Just get rid of libnosys.a, under Project / Properties / C/C++ Build / Settings / Tool Settings / MCU GCC Linker / System Calls, set Do not use system calls from library.

For the undefined reference errors, put __attribute__((used)) before each function which is missed by the linker.