Reputation: 57
I have a library file (.lib) where there are many functions. I am trying to build a shared object out of it. But non of the symbols in library are there in shared object that are in library. However, when I generate executable out of the same library the symbols are visible.
Is some thing wrong with libraries? Do I need to compile libraries in a format suitable for creating shared object?
Upvotes: 0
Views: 726
Reputation: 136208
What linkers do is they pull only those .o
files from an archive .a
that resolve an unresolved symbol.
When you link a shared library and pass only .a
files to the linker, there are no unresolved symbols, hence no object files from .a
get pulled into the shared library.
To link a shared library the object files must be compiled as position independent code (-fPIC
, .o
files for .a
are normally built without this option) and then linked into a shared library. No .a
files are necessary for that.
Another option is when linking .a
files is to explicitly undefine the symbols that are present in .a
using -Wl,--undefined=<symbol>
linker option. But this does not scale well because it requires mentioning at least one symbol from each .o
comprising .a
(assuming the linker pulls in an entire .o
, rather than just one symbol from it).
Upvotes: 3