Poiex
Poiex

Reputation: 138

Linking Boost static libraries

I am trying to compile a shared library using static libraries from Boost and OpenCV. Here below the command I am using to compile my library.

g++ -fPIC libsaliency.cpp -shared -o libsaliency.so \
                -I/home/poiesi/data/libraries/boost_1_66_0/installed_w_contrib_static/include -I/home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/include \
                -Wl,--whole-archive \
                    /home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_graph.a \
                    /home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_filesystem.a \
                    /home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_system.a \
                    /home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_core.a \
                    /home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_highgui.a \
                    /home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_imgproc.a \
                    /home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_imgcodecs.a \
                    /home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_features2d.a \
                    /home/poiesi/data/libraries/opencv-3.4.0/installed_w_contrib_static/lib/libopencv_video.a \
                -Wl,--no-whole-archive

However, I have this error:

usr/bin/ld: /home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_graph.a(read_graphviz_new.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
        /home/poiesi/data/libraries/boost_1_66_0/installed/lib/libboost_graph.a(read_graphviz_new.o): error adding symbols: Bad value
        collect2: error: ld returned 1 exit status
        Makefile:7: recipe for target 'saliency' failed
        make: *** [saliency] Error 1

Does this mean I have to recompile Boost using -fPIC command? I checked this online but I haven't found much info about it. This makes me wonder if I am searching for the right thing. Do you have any suggestion?

EDIT: As suggested below by Mike, I recompiled Boost like this:

./b2 cxxflags="-fPIC" link=static install

and I can now compile my .so library.

Upvotes: 3

Views: 2146

Answers (2)

Val
Val

Reputation: 22797

With boost 1.72.0, fixed this problem by recompile boost static library with -fPIC.

./bootstrap.sh --prefix=/usr/
sudo ./b2 cxxflags=-fPIC cflags=-fPIC link=static -a
sudo ./b2 install

Upvotes: 1

Mike Kinghan
Mike Kinghan

Reputation: 61575

Does this mean I have to recompile Boost using -fPIC command?

Yes. All code that is linked into a shared library must be Position Independent Code. Object files within static libraries normally are not, as shared libraries normally link other shared libraries.

But there is nothing in principle to stop you from building boost static libraries from -fPIC-compiled object files.

It would be simpler, of course, to link the shared versions of the boost libraries.

Upvotes: 3

Related Questions