Mixing Static Libraries and Shared Libraries

I have a project in which I have one static library libhelper.a and another with my actual library of shared objects, libtestlib.so . My goal is to link libhelper.a with libtestlib.so . Is this possible on Linux / BSD? When I tried to create a test program, I received the following errors:

./prog1:/usr/local/lib/libtestlib.so.1.0: undefined symbol ''

I assume this is because libhelper.a was not compiled with -fPIC , but libtestlib.so was. What is the correct way to create programs that use shared libraries that also have dependencies on static libraries?

Thanks!

+5
source share
1 answer

My goal is to link libhelper.a with libtestlib.so. Is this possible on Linux?

Of course. This should do:

 gcc -shared -fPIC -o libtestlib.so $(OBJS) \ -Wl,--whole-archive -lhelper -Wl,--no-whole-archive 

libhelper.a has not been compiled with -fPIC

It is best to rebuild libhelper.a with -fPIC . If this is not possible, the command above will work on Linux/ix86 , but not on. Linux/x86_64 .

What is the correct way to create programs that use shared libraries that also have dependencies on static libraries?

If you included libhelper.a in libtestlib.so as described above, simply:

 gcc main.c -ltestlib 

- that’s all you need. If you insist on contacting libhelper.a , then you must tell the end user that he should link, for example,

 gcc main.c -ltestlib -lhelper 

It is not possible to indicate that libtestlib.so dependent on libhelper.a .

+11
source

Source: https://habr.com/ru/post/902780/


All Articles