I’m about to discribe a problem that made me lose several hours searching for the problem, while the solution was quite simple.
I was working on an application that uses two static libraries, or so called archives. My application uses functions from one library, while that library uses functions from another library. This situation sound complex, but I’ll explain using an example.
helloWorld.app uses two archives
This first archive is build from:
You can compile these files to an archive using these 2 commands:
Similar library to
libArchive1.a with use of the
This will require to provide the parameter
-I so the compiler knows where to get the file
Small application printing “Hello World!” to stdout using the
Now here is where the problem did arise, compiling the application. This is what I did:
-lArchive2 will tell the compiler to link in
libArchive1. But I got the following error:
undefined reference to GetHello()
After a long search (the application I was working on was a lot more complex, so many things could have been wrong), I found the order of importing libraries with the
-l argument matter. So after switching the order of
-lArchive2, the application got compiled and did print the text “Hello World!” when running it.
The order of importing libraries matter. As far as I know it is a compiler bug and seems to be solved in newer versions (on Mac OSX 10.5 it works, on OpenSUSE 11 the error occurs). Anyhow, if you have problem with this place the least depended library last in the argument list:
This will sound really technical for a lot of people, but it took me too long solving it to not share it with you. I hope someone will benefit from it.
You can download the source code of this example here.