Static Linking Compilation for Boost C++ Libraries

Boost C++ Libraries provides a set of peer-reviewed portable C++ source libraries. As we know, coding in c++, there are a lots of things we need to write it from scratch, we need to create a common socket class, threading class, logging class, regex functions etc. Developing those libraries sometimes takes too much time, and that makes most of the programmers suffer while given a very tide time line and consequently the companies and its customers will suffer. At this point, the codes will reach End of Life, a new groups of developers comes in, reinvent the codes which may equally sucks and this infinite loops goes on.

Boost provides a set of libraries which will really saves a lots of development cost. But use boost libraries with some cares. There are few things to bare in mind while embedding boost libraries into your codes.

1. Some Boost libraries needs additional dynamic linking by defaults.
2. Different version of Boost dynamic libraries do not compatible.
3. Different version of glibc may have different set of dynamic libs for the same boost version.

For statement 1, there are libraries such as Boost.Thread and Boost.Regex uses dynamic linking by default. Fortunately boost have static linking for those boost libs too.

For statement 2, the application that needs dynamic linking compiled with Boost version A, does not able to run with dynamic lib of Boost version B. Damn! That the reason I prefer static linking.

For statement 3, In Linux environment, codes that compile with gcc 4.1 may not run in Linux with gcc 3.4 because they uses different glibc runtime libraries. But what if you are using latest workstation such as Fedora 9 and aim to compile codes to work in Red Hat ES4? You may have few set of gcc with different version. For my case, my default GCC is 4.1, but I have GCC 3.4 installed too. During compilation, I need to use gcc34 or g++34 instead. While compiling the Boost libraries, you need to specified the gcc version, so that the static and dynamic libs may based on the correct version of glibc.

Compile Boost Library support static linking, with specified gcc versions

1. Download Boost, my case, boost_1_35_0.tar.bz2.

2. ./configure
(by default libs and header files will install in /usr/local, while running ./configure, you can specified your target location with –prefix) i.e ./configure –prefix=/usr

3. Edit user-config if you are wants the to compile Boost with specific GCC version.
My case I modify the line
using gcc ;
To
using gcc : 3.4 : g++34 ;

4. If you want static linking lib for boost, you need to do full compilation. Modify Makefile after running ./configure
BJAM_CONFIG=–build-type=complete

5, make; sudo make install

Boost lib will be installed to :
/usr/local/lib/libboost_*
Boost includes will be located at:
/usr/local/include/boost-1_35

Compile codes using Boost.Thread Static library
This is a simple example on how you compile your codes what uses Boost.Thread static lib.

g++34 -o myApp{,.cc} -I/usr/local/include/boost-1_35/ -L/usr/local/lib -lboost_thread-gcc34-mt-s -lpthread

(Bare in mind if you are using static linking, you need to specified -lpthread, if you are using dynamic linking, you may ignore -lpthread.)

Enjoy hacking with boost!