Search code examples
linuxgccboostb2

How to compile Boost as a dynamic library with static stdc++ and gcc links on Linux?


I am looking to compile Boost on Linux as a shared library (so .\b2 install link=shared) but with a static link to libstdc++ and libgcc.

Without any modifications to the above command, I naturally see a dynamic link to those two libraries:

 ldd libboost_filesystem.so
        linux-vdso.so.1 (0x00007ffebd5ef000)
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f2f8330d000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f2f832f2000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2f832cf000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2f830dd000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f2f82f8e000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f2f83521000)

The linker flags in question that would statically link gcc and stdc++ are -static-libgcc and -static-libstdc++. I have used them in with CMake projects and they work as expected (I just add during configuration -DCMAKE_SHARED_LINKER_FLAGS="-static-libgcc -static-libstdc++").

With the Boost b2 install script, I've throwing those flags everywhere to hedge my bets:

./b2 install cxxflags="-static-libgcc -static-libstdc++" cflags="-static-libgcc -static-libstdc++" linkflags="-static-libgcc -static-libstdc++"

to no avail.

I also tried:

./b2 install gcc/linkflags="-static-libgcc -static-libstdc++" gcc/cxxflags="-static-libgcc -static-libstdc++" gcc/cflags="-static-libgcc -static-libstdc++"

and

./b2 install gcc/linkflags="-static-libgcc -static-libstdc++" gcc/cxxflags="-static-libgcc -static-libstdc++" gcc/cflags="-static-libgcc -static-libstdc++" gcc/link=static

What am I doing wrong? I'm obviously missing something about those particular flags. How can I properly use those flags?


Solution

  • You have to provide -static-libgcc and -static-libstdc++ flags as linkflags

    First download and extract the Boost source files then go to the Boost directory and run ./bootstrap.sh then modify the project-config.json file to add this at the end using gcc : : : <linkflags>-static-libgcc <linkflags>-static-libstdc++ ;

    Now you can build and install the Boost libraries with this command ./b2 install link=shared