I’m working on a project with an ATmega328 and using avr-gcc/g++. I'm trying to enable Link Time Optimization (LTO), but I’m encountering issues during linking.
Here's a simplified example:
• Static Library: libATmega328__T_LIB.a (consists of blink.cpp and blink.cpp.obj)
• Executable: ATmega328__T_ uses the blink() function defined in blink.cpp.obj within main.cpp.
Compiler Flags:
tools.build:cxxflags=['-mmcu=atmega328', '-O2', '-ffunction-sections', '-fdata-sections', '-fno-exceptions', '-fno-rtti', '-flto', '-fuse-linker-plugin', '-fno-devirtualize']
Linker Flags:
tools.build:exelinkflags=['-Wl,--gc-sections', '-flto'] tools.build:sharedlinkflags=['-Wl,--gc-sections', '-flto']
Build Environment:
AR=/opt/avr8-gnu-toolchain-linux_x86_64/bin/avr-ar
AS=/opt/avr8-gnu-toolchain-linux_x86_64/bin/avr-as
RANLIB=/opt/avr8-gnu-toolchain-linux_x86_64/bin/avr-ranlib
CC=/opt/avr8-gnu-toolchain-linux_x86_64/bin/avr-gcc
CXX=/opt/avr8-gnu-toolchain-linux_x86_64/bin/avr-g++
STRIP=/opt/avr8-gnu-toolchain-linux_x86_64/bin/avr-strip
When I try to link the project, I get the following errors:
[ 60%] Linking CXX static library libATmega328__T_LIB.a
/opt/avr8-gnu-toolchain-linux_x86_64/bin/avr-ar: CMakeFiles/ATmega328__T_LIB.dir/blink.cpp.obj: plugin needed to handle lto object
/opt/avr8-gnu-toolchain-linux_x86_64/bin/avr-ranlib: blink.cpp.obj: plugin needed to handle lto object
[100%] Linking CXX executable ATmega328__T_
/tmp/ccWiGx4b.ltrans0.ltrans.o: In function `main':
<artificial>:(.text.startup.main+0x0): undefined reference to `blink()'
collect2: error: ld returned 1 exit status
It seems that the plugin for LTO isn’t being used correctly, even though I have specified -flto in the flags. Does anyone have an idea on how to resolve this issue?
This work for me:
tools.build:cflags=["-mmcu=atmega328", "-O2", "-ffunction-sections", "-fdata-sections", "-fno-exceptions", "-fno-rtti", "-flto", "-ffat-lto-objects"] tools.build:cxxflags=["-mmcu=atmega328", "-O2", "-ffunction-sections", "-fdata-sections", "-fno-exceptions", "-fno-rtti", "-flto", "-ffat-lto-objects"] tools.build:sharedlinkflags=["-Wl,--gc-sections", "-flto", "-ffat-lto-objects"] tools.build:exelinkflags=["-Wl,--gc-sections", "-flto", "-ffat-lto-objects"]
One more problem was, that i use conan profile in connection with cmake. Unfortunately, the build flags from the conan profile are not automatically passed on to the CMake Build flags, which I then attached from the conan toolchain.cmake.