Static Linking on Linux
Gideon Weems

I generally don't recommend static linking, but it does have its uses. Too bad I can't get it to work.

I tried this:

g++ `pkg-config --libs --static allegro-static-5 allegro_audio-static-5 allegro_acodec-static-5 allegro_image-static-5 allegro_primitives-static physfs allegro_physfs-static` game.cpp -o game_static

... and then with the #define:

g++ -DALLEGRO_STATICLINK `pkg-config --libs --static allegro-static-5 allegro_audio-static-5 allegro_acodec-static-5 allegro_image-static-5 allegro_primitives-static physfs allegro_physfs-static` game.cpp -o game_static

I tried this:

g++ -DALLEGRO_STATICLINK /usr/lib/liballegro-static.a /usr/lib/liballegro_audio-static.a /usr/lib/liballegro_acodec-static.a /usr/lib/liballegro_image-static.a /usr/lib/liballegro_primitives-static.a /usr/lib/libphysfs.a /usr/lib/liballegro_physfs-static.a game.cpp -o game_static

... and with a "-static:"

g++ -DALLEGRO_STATICLINK -static /usr/lib/liballegro-static.a /usr/lib/liballegro_audio-static.a /usr/lib/liballegro_acodec-static.a /usr/lib/liballegro_image-static.a /usr/lib/liballegro_primitives-static.a /usr/lib/libphysfs.a /usr/lib/liballegro_physfs-static.a game.cpp -o game_static

I also tried this (same thread):

g++ -DALLEGRO_STATICLINK -Wl,--start-group `pkg-config --libs --static allegro-static-5 allegro_audio-static-5 allegro_acodec-static-5 allegro_image-static-5 allegro_primitives-static physfs allegro_physfs-static` -Wl,--end-group game.cpp -o game_static

The only thing that resulted from any of this was linker errors up the wazoo. I am tired. Someone please help.

Static linking with A4 on Windows was so easy...

Appendix

■ pwd
/usr/lib
■ find -iname "*allegro*" | grep static
./liballegro_font-static.a
./liballegro_main-static.a
./liballegro_physfs-static.a
./liballegro_audio-static.a
./liballegro_memfile-static.a
./pkgconfig/allegro_main-static-5.pc
./pkgconfig/allegro_color-static-5.pc
./pkgconfig/allegro_memfile-static-5.pc
./pkgconfig/allegro_dialog-static-5.pc
./pkgconfig/allegro_audio-static-5.pc
./pkgconfig/allegro-static-5.pc
./pkgconfig/allegro_acodec-static-5.pc
./pkgconfig/allegro_image-static-5.pc
./pkgconfig/allegro_font-static-5.pc
./pkgconfig/allegro_physfs-static-5.pc
./pkgconfig/allegro_primitives-static-5.pc
./pkgconfig/allegro_ttf-static-5.pc
./liballegro_primitives-static.a
./liballegro_acodec-static.a
./liballegro_color-static.a
./liballegro_image-static.a
./liballegro-static.a
./liballegro_dialog-static.a
./liballegro_ttf-static.a

■ ldd game_dynamically_linked
linux-vdso.so.1 =>  (0x00007ffffc2f7000)
liballegro.so.5.1 => /usr/lib/liballegro.so.5.1 (0x00007f84b0db4000)
liballegro_audio.so.5.1 => /usr/lib/liballegro_audio.so.5.1 (0x00007f84b0b9b000)
liballegro_acodec.so.5.1 => /usr/lib/liballegro_acodec.so.5.1 (0x00007f84b0992000)
liballegro_image.so.5.1 => /usr/lib/liballegro_image.so.5.1 (0x00007f84b0788000)
liballegro_primitives.so.5.1 => /usr/lib/liballegro_primitives.so.5.1 (0x00007f84b0572000)
libphysfs.so.1 => /usr/lib/libphysfs.so.1 (0x00007f84b0347000)
liballegro_physfs.so.5.1 => /usr/lib/liballegro_physfs.so.5.1 (0x00007f84b0143000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f84afe3c000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f84afbb9000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f84af9a3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f84af618000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f84af3fb000)
libSM.so.6 => /usr/lib/x86_64-linux-gnu/libSM.so.6 (0x00007f84af1f4000)
libICE.so.6 => /usr/lib/x86_64-linux-gnu/libICE.so.6 (0x00007f84aefd9000)
libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f84aec9d000)
libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f84aea8b000)
libXcursor.so.1 => /usr/lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f84ae881000)
libXinerama.so.1 => /usr/lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f84ae67e000)
libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f84ae476000)
libGLU.so.1 => /usr/lib/x86_64-linux-gnu/libGLU.so.1 (0x00007f84ae209000)
libGL.so.1 => /usr/lib/x86_64-linux-gnu/libGL.so.1 (0x00007f84ae004000)
/lib64/ld-linux-x86-64.so.2 (0x00007f84b10d8000)
libasound.so.2 => /usr/lib/x86_64-linux-gnu/libasound.so.2 (0x00007f84add10000)
libFLAC.so.8 => /usr/lib/x86_64-linux-gnu/libFLAC.so.8 (0x00007f84adac3000)
libogg.so.0 => /usr/lib/x86_64-linux-gnu/libogg.so.0 (0x00007f84ad8bd000)
libdumb.so.1 => /usr/lib/x86_64-linux-gnu/libdumb.so.1 (0x00007f84ad688000)
libvorbisfile.so.3 => /usr/lib/x86_64-linux-gnu/libvorbisfile.so.3 (0x00007f84ad47e000)
libvorbis.so.0 => /usr/lib/x86_64-linux-gnu/libvorbis.so.0 (0x00007f84ad251000)
libpng12.so.0 => /lib/x86_64-linux-gnu/libpng12.so.0 (0x00007f84ad029000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f84ace12000)
libjpeg.so.8 => /usr/lib/x86_64-linux-gnu/libjpeg.so.8 (0x00007f84acbd8000)
libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007f84ac9d2000)
libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f84ac7b2000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f84ac5ae000)
libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f84ac3a4000)
libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f84ac19e000)
libatiuki.so.1 => /usr/lib/x86_64-linux-gnu/libatiuki.so.1 (0x00007f84ac094000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f84abe8c000)
libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f84abc89000)
libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f84aba83000)

SiegeLord

The only thing that resulted from any of this was linker errors up the wazoo.

You neglected to attach them. I compile static binaries with essentially the same invocation (without the #define though), and it works fine. Also, paste what one of those pkg-config invocations yields (just run it on the command line without the backticks).

Slartibartfast

I think you are missing libraries that allegro depends on. For example IIRC the image addon depends on libpng and libjpeg.

My current project is linked in Linux with:

unix:!macx: LIBS += -lallegro_monolith-static
# Allegro's dependencies
unix:!macx: LIBS += -lXcursor
unix:!macx: LIBS += -lX11
unix:!macx: LIBS += -lpthread
unix:!macx: LIBS += -lGL
unix:!macx: LIBS += -lpng   # Allegro Image Addon
unix:!macx: LIBS += -ljpeg  # Allegro Image Addon

Though the list is likely to grow longer the more stuff I/you use from Allegro.
Just look at the linking error, google it and find the missing library.

SiegeLord

I think you are missing libraries that allegro depends on. For example IIRC the image addon depends on libpng and libjpeg.

This should not be necessary to add manually.

$ pkg-config --libs --static allegro-static-5 \
allegro_audio-static-5 allegro_acodec-static-5 allegro_image-static-5 \
allegro_primitives-static-5

-L/usr/local/lib -lallegro_acodec-static -ldumb -lvorbisfile
-lvorbis -logg -lallegro_audio-static -lpulse-simple -lpulse -lasound
-lallegro_image-static -lpng -lz -ljpeg -lallegro_primitives-static
-lallegro-static -lm -lpthread -lXcursor -lXinerama -lXrandr -lGLU
-lGL -lSM -lICE -lX11 -lXext

Slartibartfast

True for

g++ `pkg-config --libs --static allegro-static-5 allegro_audio-static-5 allegro_acodec-static-5 allegro_image-static-5 allegro_primitives-static physfs allegro_physfs-static` game.cpp -o game_static

Not true for

Quote:

g++ -DALLEGRO_STATICLINK /usr/lib/liballegro-static.a /usr/lib/liballegro_audio-static.a /usr/lib/liballegro_acodec-static.a /usr/lib/liballegro_image-static.a /usr/lib/liballegro_primitives-static.a /usr/lib/libphysfs.a /usr/lib/liballegro_physfs-static.a game.cpp -o game_static

... and with a "-static:"

g++ -DALLEGRO_STATICLINK -static /usr/lib/liballegro-static.a /usr/lib/liballegro_audio-static.a /usr/lib/liballegro_acodec-static.a /usr/lib/liballegro_image-static.a /usr/lib/liballegro_primitives-static.a /usr/lib/libphysfs.a /usr/lib/liballegro_physfs-static.a game.cpp -o game_static

Which is what I looked at :)

I guess we need to examine the output of pkg-config as well as the linking errors :)

SiegeLord

Hah, indeed. I guess I tl;dr'd those long command lines >_<.

Gideon Weems

I'm sorry, guys. I'm still a bit new to the Linux development environment, and it seems like I didn't configure pkg-config correctly:

■ pkg-config --libs --static allegro-static-5 allegro_audio-static-5 allegro_acodec-static-5 allegro_image-static-5 allegro_primitives-static physfs allegro_physfs-static
Package allegro_primitives-static was not found in the pkg-config search path.
Perhaps you should add the directory containing `allegro_primitives-static.pc'
to the PKG_CONFIG_PATH environment variable
No package 'allegro_primitives-static' found
Package physfs was not found in the pkg-config search path.
Perhaps you should add the directory containing `physfs.pc'
to the PKG_CONFIG_PATH environment variable
No package 'physfs' found
Package allegro_physfs-static was not found in the pkg-config search path.
Perhaps you should add the directory containing `allegro_physfs-static.pc'
to the PKG_CONFIG_PATH environment variable
No package 'allegro_physfs-static' found

At least I know where to look now. I'll report back when everything's working.

Slartibartfast

I think it should be taken care of automatically as part of sudo make install. Maybe you should check if there's a typing error, for example you have allegro-static-5 but allegro_acodec-static-5. I don't remember which it should be, but the inconsistency is interesting. You can also use locate static.pc or locate primitives-static.pc to see if those files exist and are in the right place.

Thomas Fjellstrom

pkg-config doesn't look in /usr/local by default. which is where allegro installs to by default. so you either have to add it to the pkg-config path when using it, or install allegro to /usr.

SiegeLord

In this case (assuming that's the actual line), the error is the missing '-5' in the primitives and physfs addons :P.

I also don't think there's a 'physfs' pc file.

Gideon Weems

All good points. Before I answer them, though, I have one more question:

... Are you guys ready for the biggest facepalm of the year?

A word of warning: While I deserve the brunt of palm, each of us takes his own share.

EDIT:

Well, I hoped that somebody would take the opportunity to redeem himself here, but I guess not. For future readers: Link libraries last when compiling. It's the most basic of basic rules, but I forgot. And three, good programmers didn't spot it.

g++ game.cpp `pkg-config --libs --static allegro-static-5` -o game_static

Thomas Fjellstrom

Yeah, I typically put linking at the very end, even after -o. it's not something that has actually affected me in YEARS.

Thread #614352. Printed from Allegro.cc