Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 875
Poiex Avatar asked Aug 27 '26 05:08

Poiex


1 Answers

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.

like image 120
Mike Kinghan Avatar answered Aug 31 '26 01:08

Mike Kinghan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!