Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Linking static libraries: undefined reference

Credits go to Cassio Renan and Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Linking static libraries: undefined reference
Lil.sen
Member #15,650
June 2014

Hello Allegro forums :) I'm a new programmer, and after getting over a few bumps on the road I finished my first small game. The next step is for me to try to make it playable on machines that do not have Allegro installed (I just want to send it to a friend, nothing too professional :P) What I did was to create the main game folder, one subdirectory /lib with the libraries I'm using, and /src with the source files and the Makefile. The first part of Makefile itself is looking something like:

#SelectExpand
1OBJ = file1.o file2.o file3.o file4.o 2LIBS_PATH = -L../lib 3LIBS = -lallegro-static -lallegro_font-static -lallegro_ttf-static -lallegro_image-static -lallegro_dialog-static -lallegro_primitives-static 4 5Game: $(OBJ) 6 g++ -o Game $(LIBS_PATH) $(LIBS) $(OBJ) 7 8-include dependencies

When entering "make", the terminal gives me an undefined reference for every single Allegro function in all the 4 files. What I attempted before writing this thread and bother you people was the following:

1) Made sure the libraries are all in /libs (they are)
2) redownloaded Allegro and rebuilt it again making sure "SHARED" was "OFF"
3) double-checked the order I'm loading the libraries, I might be getting this wrong but it's the same order my game loads them in so that shouldn't be an issue?

As far as I can tell, it shouldn't be a problem within the code itself as the game loads and plays properly when trying to compile it manually and with the default library location.

Thank you! I'm sure it's something silly that I'm missing :-/

Cassio Renan
Member #14,189
April 2012
avatar

When linking statically, you have also to link against all of the allegro dependencies.
On Windows, that would be:

gdiplus uuid kernel32 winmm psapi opengl32 glu32 user32 comdlg32 gdi32 shell32 ole32 advapi32 ws2_32 shlwapi

Not counting some of the optionals, like vorbis, ogg, dumb, freetype, etc.

Edgar Reynaldo
Member #8,592
May 2007
avatar

gcc links in the order you tell it to. So I think you need to put $(LIBS) after $(OBJ).

Lil.sen
Member #15,650
June 2014

gcc links in the order you tell it to. So I think you need to put $(LIBS) after $(OBJ).

That did work! It gives me a bunch of new errors, but they seem to be linked to functions that Allegro itself uses, which I think it's because as Cassion Renan said, I have to link all Allegro dependencies.

I'm on Ubuntu though so I can't link these that you posted. I'll try to see what I can do about it now :)

EDIT: Alright, it seems that I need to link a few libraries, but I'm having a hard time with it. For example, libgl1-mesa-dev seems to be composed by different files, and the actual library file seems to be .so (libGL) while my Makefile works just if I link static libraries... Do I need to link just that one file, and if so, will it be enough just to use the -static prefix while compiling? Thanks again.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Next, try pkg-config :

$(pkg-config --libs --static allegro-static-5.0)

listing all the other allegro modules you used. There may be some way to use a static monolith as well. You can echo the same command to see what libraries it links to.

Lil.sen
Member #15,650
June 2014

I had to manually set the PKG_CONFIG_PATH variable but now I can echo it, and it does me the list of libraries each module depends on. So if I understood this correctly what I should do is find them, copy them all in my /lib folder and add them to my Makefile?

Edgar Reynaldo
Member #8,592
May 2007
avatar

No, when you link statically the code is actually included in the executable instead of in the dll. That's what the linker is for. There are lib*.so files and these are the linux equivalent of a dll. When you link statically you don't need the .so files anymore. (shared object)

Lil.sen
Member #15,650
June 2014

Yep, everything is working just fine. I hope nothing will break down when I try to run this on a different machine ::) Thanks a lot for the help, I'll tag this as answered now.

Go to: