views:

1128

answers:

2

I have a C++ library and a C++ application trying to use functions and classes exported from the library. The library builds fine and the application compiles but fails to link. The errors I get follow this form:

app-source-file.cpp:(.text+0x2fdb): undefined reference to `lib-namespace::GetStatusStr(int)'

Classes in the library seem to be resolved just fine by the linker, but free functions and exported data (like a cosine lookup table) invariably result in the above error.

I am using Ubuntu 8.04 (Hardy), and it is up to date with the latest Unbuntu packages.

The command to link the library is (with other libraries removed):

g++ -fPIC -Wall -O3 -shared -Wl,-soname,lib-in-question.so -o ~/project/lib/release/lib-in-question.so

The command to link the application is (with other libraries removed):

g++ -fPIC -Wall -O3  -L~/project/lib/release -llib-in-question -o ~/project/release/app-in-question

Finally, it appears (as best as I can tell) that the symbols in question are being exported properly:

nm -D ~/project/lib/release/lib-in-question.so | grep GetStatusStr --> U _ZN3lib-namespace12GetStatusStrEi
+6  A: 

the U before _ZN3lib-namespace12GetStatusStrEi in the nm output shows that the symbol is undefined in the library.

Maybe it's defined in the wrong namespace: it looks like you're calling it in lib-namepace but you might be defining it in another.

PiedPiper
Any suggestion on how to get the symbol defined? The cpp file it lives in is definitely being compiled!
Clay
I just edited my answer: maybe you used it in the wrong namespace
PiedPiper
You are correct -- it was a namespace issue
Clay
+2  A: 

It's been a while, but if you specify a lib with the -l option, then don't you have the skip the lib prefix?

(I changed the name from "lib-in-question.so" to "libfoobar.so" for easier reading for the example below)

g++ -fPIC -Wall -O3  -L~/project/lib/release -lfoobar

or

g++ -fPIC -Wall -O3  ~/project/lib/release/libfoobar.so
Christian.K
My makefile has that bit right: the library is libzclocksupport but the linker command is -lzclocksupport
Clay