Link static library to shared lib?

Background:

I want to link some static libs in a shared lib. The reason is because I want my application to use specific versions of lib with which I tested it. I do not want to send static versions as shared libraries with my application. I created this lib sample and application to simplify as much as possible. I would like to continue linking the generic lib to the application during linking.

Question:

Why am I getting the error messages listed below? What am I doing wrong? This may not be the usual way to do something on Linux, but is it possible? Is this promotion specific?

---- library

//example.cpp #include <boost/thread.hpp> void doit() { boost::thread t1; } #build script g++ -Wall -fPIC -I/usr/include -c example.cpp -o example.o g++ -shared /usr/lib/libboost_thread.a /usr/lib/libboost_system.a example.o -o libexample.so #build OK. 

---- sample application

 //main.cpp #include <iostream> void doit(); int main() { std::cout << "main\n"; doit(); return 0; }; #build script. g++ -Wall -c main.cpp -o main.o g++ libexample.so main.o -o main #error message. libexample.so: undefined reference to `boost::thread::thread()' libexample.so: undefined reference to `boost::thread::~thread()' collect2: ld returned 1 exit status 

All source code is in the same directory. Boost is installed in / usr / lib and / usr / include. Boost version 1.40 was installed using apt-get on an ubuntu 10.04 machine.

Thanks!

+4
source share
1 answer

I think the easiest way for you is to use the linker switch --whole-archive (there are more SO questions on this topic, see how to link a static library to a dynamic library in gcc here ).

The disadvantage of this is that your shared library will export all characters from the static Boost libraries, and you might have strange problems if you use your .so in an application that also uses Boost (but a different version or compiled with different switches) .

So, you need to use the script version to hide what is exported from the library (see How to hide the name of exported characters in the shared library , and also Google for the linker version scripts), leaving only doit() visible. In your case, this version of the script might look like this:

 { global: doit*; local: *; } 

You also need to make sure that the static libraries you are linking to are compiled with -fPIC (which is unlikely if you did not configure your build flags), in other words, you will have a performance limitation on i386 and cannot link to amd64 at all.

+1
source

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


All Articles