Search code examples
c++boostcmakeshared-libraries

version-independent dynamic linking with boost libraries


question

How can I compile a shared library linking with version-independent boost shared library? My cmakelists.txt is like as follows

find_package(Boost REQUIRED COMPONENTS serialization)
...
target_link_libraries(_omplpy PRIVATE ${Boost_LIBRARIES} ${otherdeps})

And, after compiling this, I checked dependency by ldd command and it shows that only the dependency of boost libraries are too specific (seems that version 1.71.0 is specified, though other libraries does not care about the minor version)

h-ishida@stone-jsk:~/python/ompl-python-thin$ ldd build/_omplpy.cpython-38-x86_64-linux-gnu.so 
    linux-vdso.so.1 (0x00007ffd34ca9000)
    libboost_serialization.so.1.71.0 => /lib/x86_64-linux-gnu/libboost_serialization.so.1.71.0 (0x00007f208012f000)
    libboost_filesystem.so.1.71.0 => /lib/x86_64-linux-gnu/libboost_filesystem.so.1.71.0 (0x00007f2080111000)
distir  libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f20800ee000)
    libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f207ff0c000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f207fdbd000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f207fda0000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f207fbae000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f20812a6000)

The problem is, boost libraries version are different for different ubuntu distributions, thus my compiled shard library _omplpy cannot be used in different distribution.

context (maybe unrelated)

I am trying to distribute a python package where a shared library linked with boost stuff is inside. Because python wheel (binary version of package) is only python-version (like 2.7, 3.8), os (mac, windows, ldistirinux), and archtecture dependent (like x86_64, aarch64), it seems impossible to distribute packages dependent on specific ubuntu distribution. For your information, the package mentioned is https://github.com/HiroIshida/ompl-thin-python and corresponding CMakeLists.txt is here https://github.com/HiroIshida/ompl-thin-python/blob/master/CMakeLists.txt


Solution

  • How can I compile a shared library linking with version-independent boost shared library?

    You can't, because the different boost libraries are not ABI-compatible. If you somehow succeeded in linking your library with "version-independent boost", the result would be a runtime crash.

    Read about the reasons for external library versioning here.

    P.S.

    I decided to build static boost library with -fPIC option and compile the target library with linking that.

    That is much easier, but unless you are very careful with symbol hiding, this approach will cause runtime crashes (if you are lucky) as soon as someone tries to use your python package in program which uses a different version of boost libraries (due to symbol collision and ABI incompatibility). And if you are unlucky, symbol collision may cause other very hard to find bugs.