This is an old question, but no one seems to have mentioned this.
You were getting lucky that the thing was linking at all.
You needed to change
g++ -g -Wall -o my_binary -L/my/dir -lfoo bar.cpp
to this:
g++ -g -Wall -o my_binary -L/my/dir bar.cpp -lfoo
Your linker keeps track of symbols it needs to resolve. If it reads the library first, it doesn't have any needed symbols, so it ignores the symbols in it. Specify the libraries after the things that need to link to them so that your linker has symbols to find in them.
Also, -lfoo
makes it search specifically for a file named libfoo.a
or libfoo.so
as needed. Not libfoo.so.0
. So either ln
the name or rename the library as appopriate.
To quote the gcc man page:
-l library
...
It makes a difference where in the command you
write this option; the linker searches and processes
libraries and object files in the order they are
specified. Thus, foo.o -lz bar.o searches library z
after file foo.o but before bar.o. If bar.o refers
to functions in z, those functions may not be loaded.
Adding the file directly to g++
's command line should have worked,
unless of course, you put it prior to bar.cpp
, causing the linker
to ignore it for lacking any needed symbols, because no symbols were needed yet.