PlatformIO Community

Pre-compiled libraries getting removed from build flags

When I try to add a pre-compiled library like this

build_flags =
  ${common.build_flags}
  -lrf24-bcm

as suggested by https://docs.platformio.org/en/latest/projectconf/section_env_build.html#projectconf-build-flags, this does not work. When I do pio remote run -v, I see that the compiler is called without that flag, even though all other build flags are correctly applied. Some playing around unvealed that basically everything that is written behind a -l flag is getting deleted from the build args, everything else is getting correctly applied.

So how could I add a pre-compiled library now?

Just as you did. It is correct that the flag does not show up during the compilation of the .o object files. It’s a linker flag. It will be included in the very last command.

Check the verbose compilation output of a blinky-example for the Uno with the platformio.ini

[env:uno]
platform = atmelavr
board = uno
framework = arduino
build_flags = 
	-lrf24-bcm
avr-gcc -o .pio\build\uno\FrameworkArduino\wiring_shift.c.o -c -std=gnu11 -fno-fat-lto-objects -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega328p -DPLATFORMIO=50005 -DARDUINO_AVR_UNO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -IC:\Users\Max\.platformio\packages\framework-arduino-avr\cores\arduino -IC:\Users\Max\.platformio\packages\framework-arduino-avr\variants\standard C:\Users\Max\.platformio\packages\framework-arduino-avr\cores\arduino\wiring_shift.c
avr-gcc-ar rc .pio\build\uno\libFrameworkArduino.a .pio\build\uno\FrameworkArduino\CDC.cpp.o .pio\build\uno\FrameworkArduino\HardwareSerial.cpp.o .pio\build\uno\FrameworkArduino\HardwareSerial0.cpp.o .pio\build\uno\FrameworkArduino\HardwareSerial1.cpp.o .pio\build\uno\FrameworkArduino\HardwareSerial2.cpp.o .pio\build\uno\FrameworkArduino\HardwareSerial3.cpp.o .pio\build\uno\FrameworkArduino\IPAddress.cpp.o .pio\build\uno\FrameworkArduino\PluggableUSB.cpp.o .pio\build\uno\FrameworkArduino\Print.cpp.o .pio\build\uno\FrameworkArduino\Stream.cpp.o .pio\build\uno\FrameworkArduino\Tone.cpp.o .pio\build\uno\FrameworkArduino\USBCore.cpp.o 
.pio\build\uno\FrameworkArduino\WInterrupts.c.o .pio\build\uno\FrameworkArduino\WMath.cpp.o .pio\build\uno\FrameworkArduino\WString.cpp.o .pio\build\uno\FrameworkArduino\abi.cpp.o .pio\build\uno\FrameworkArduino\hooks.c.o .pio\build\uno\FrameworkArduino\main.cpp.o .pio\build\uno\FrameworkArduino\new.cpp.o .pio\build\uno\FrameworkArduino\wiring.c.o .pio\build\uno\FrameworkArduino\wiring_analog.c.o .pio\build\uno\FrameworkArduino\wiring_digital.c.o .pio\build\uno\FrameworkArduino\wiring_pulse.S.o .pio\build\uno\FrameworkArduino\wiring_pulse.c.o .pio\build\uno\FrameworkArduino\wiring_shift.c.o
avr-gcc-ranlib .pio\build\uno\libFrameworkArduino.a
avr-g++ -o .pio\build\uno\firmware.elf -Os -mmcu=atmega328p -Wl,--gc-sections -flto -fuse-linker-plugin .pio\build\uno\src\main.cpp.o -L.pio\build\uno -Wl,--start-group .pio\build\uno\libFrameworkArduinoVariant.a 
.pio\build\uno\libFrameworkArduino.a -lrf24-bcm -lm -Wl,--end-group

As you can see, only the final linker command includes -lrf24-bcm, which is correct.

If you want to link in precompiled libraries, also don’t forget to add -L path flag to point to the path where the .a library is.

Thank you! I was completely on the wrong track, the cause that it did not work was that I referenced the library wrongly in my include (RF24.h instead of RF24/RF24.h). Now it works as expected :slight_smile: