Static linking of GraphicsMagick++ library


I’m trying to statically link an external library (GraphicsMagick++) into my C++ application (running on Rapsberry Pi with Raspberry Pi OS).

The project compiles, and the library works on my development Pi, but when I try to run it on another (clean installed) Pi it complains that it cannot find the library.

It seems to me that PIO is linking the library dynamically instead of static. When I run “ldd” on my executable it also shows the GraphicsMagic dependency:

pi@RPIZ2306:~/projects/ledboardpi/runtime $ ldd ./program (0x0000007fa3f36000) => /lib/aarch64-linux-gnu/ (0x0000007fa3d56000)
—> libGraphicsMagick+± => /lib/libGraphicsMagick+± (0x0000007fa3cd0000) => /lib/aarch64-linux-gnu/ (0x0000007fa3af8000) => /lib/aarch64-linux-gnu/ (0x0000007fa3a4d000) => /lib/aarch64-linux-gnu/ (0x0000007fa3a29000) => /lib/aarch64-linux-gnu/ (0x0000007fa39f8000) => /lib/aarch64-linux-gnu/ (0x0000007fa3883000) => /lib/aarch64-linux-gnu/ (0x0000007fa3845000)
/lib/ (0x0000007fa3f06000)
—> => /lib/ (0x0000007fa3579000) => /lib/aarch64-linux-gnu/ (0x0000007fa355c000) => /lib/aarch64-linux-gnu/ (0x0000007fa34fa000) => /lib/aarch64-linux-gnu/ (0x0000007fa34e1000)

The library itself is installed in “/home/pi/libs/GraphicsMagick-1.3.40”, the PlatformIO project is located in “/home/pi/projects/led”

This is (the relevant part of) my PlatformIO.ini file:

   build_flags = 
  -static -llibGraphicsMagick++              

How can I link this library into my executable to avoid dependencies on the target machine?


Unfortunately, when you do

build_flags = -static

it only affects the compilation stage (.c/.cpp- > .o), not the linking stage (.o, .a.elf).

gcc -o .pio/build/native/src/main.o -c -static -DPLATFORMIO=60110 -Iinclude -Isrc src/main.c
gcc -o .pio/build/native/program .pio/build/native/src/main.o -L.pio/build/native

The resulting binary is still linked dynamically.

max@virtualbox:~/temp/static_ex$ ldd .pio/build/native/program (0x00007fffba9f7000) => /lib/x86_64-linux-gnu/ (0x00007fc5d3800000)
	/lib64/ (0x00007fc5d3a89000)

Instead, you have to do this in your platformio.ini:

platform = native
extra_scripts =

with being a file at the same level as the platformio.ini with content


This results in

gcc -o .pio/build/native/src/main.o -c -DPLATFORMIO=60110 -Iinclude -Isrc src/main.c
gcc -o .pio/build/native/program -static .pio/build/native/src/main.o -L.pio/build/native

and as confirmed by ldd,

max@virtualbox:~/temp/static_ex$ ldd .pio/build/native/program 
	is not a dynamic executable

Thank you very much for the detailed explanation!

This indeed is a solution to instruct the linker to link the libraries statically.

However, now another question pops up: how can I enable static linking for selected libraries and keep dynamic linking for others?

At the core, that’s a GCC/Linker question. I think this answer would work there:

You should be able to give the required flags as an array ito LINKFLAGS like in the original example above. If LINKFLAGS doesn’t work with the needed order of the flags, you might be able to fall back to this method.

Actually, when I read that Stackoverflow anaswer again, it says that simply giving the full path to the .a file should link it statically. Meaning if you do a script like


might work too. (File is class auto-defined in the SCons build system. The full path may be given instead of the relative path. Make sure to check the project tasks → Advanced → Verbose Build to see the full compiler invocations.)

Using the full path to the library works. Note: The full path! I tried with relative paths but then the linker returns an error.

Besides the GraphicsMagic++ lib, I also added the main Magick library, but then I get a bunch of “undefined references”, eventough the search path is correctly set.

/usr/bin/ld: /home/pi/libs/GraphicsMagick-1.3.40/magick/.libs/libGraphicsMagick.a(libGraphicsMagick_la-operator.o): in function `QuantumPowCB':
/home/pi/libs/GraphicsMagick-1.3.40/magick/operator.c:1575: undefined reference to `GOMP_critical_name_start'
/usr/bin/ld: /home/pi/libs/GraphicsMagick-1.3.40/magick/operator.c:1575: undefined reference to `GOMP_critical_name_end'

I guess I might have messed something up in my experimenting and building, rebuilding, installing & removing the libraries multiple times.
I’ll try again from a clean install and give an update then.

Thanks once again Max

This linking issue might arrive from a wrong linking order – try swapping the graphicsmagic++ and magick library positions in the array.