.so fails with lib, but runs with .o

Hi

I have a project which uses shared object libraries.
There are other parts of the program which i must link statically.

When i link my program where the static parts are
linked directly as .o files, my program works correctly.

If i create a library (libKernel.a) from these object files,
and link the program with it, then the program fails with
an error message
1
2
Couldn't load library or extract symbols [./plugins/qhg_SpecialDynObj.so]:
  ./plugins/qhg_SpecialDynObj.so: undefined symbol: _ZN12AgentFactory13doCreateAgentEttf 

Now the function "doCreateAgent" is in the library libKernel.a:
1
2
 $ nm libKernel.a | grep doCreateAgent
00000000000001d0 T _ZN12AgentFactory13doCreateAgentEttf


And it looks exactly like the function the .so file is looking for:
1
2
 $ nm ../plugins/qhg_VegDynObj.so | grep doCreateAgent
                 U _ZN12AgentFactory13doCreateAgentEttf


When i link my program with the library libKernel.a *and* the object file AgentFactory.o
then the linking succeeds (even though the functions of AgentFactory are linked in twice)
and furthermore the program runs correctly (i.e. no more "undefined symbol")

Can anybody explain this and help me getting it to run?

Thank You
Jody
Last edited on
The linux linker throws away unused symbols from static libraries. Because the executable itsself doesn't use that symbol, the compiler thinks it's unused, and so it's not there when the dynamic library later comes to look for it.

You can force the linker not to throw away unused symbols with the whole-archive linker option.
Thanks!
That explained and solved my problem.

For the sake of completeness, this is how the linking must look:
g++ myprog.cpp -o myprog -Wl,--whole-archive -lKernel -Wl,--no-whole-archive
The -Wl transfers the --whole-archive option to the linker (gcc doesn't know this option).
The option --no-whole-archive is needed so that following libraries may still be stripped of unnecessary objects.
Topic archived. No new replies allowed.