Howto link in boost::test on select platforms and get smaller test executables

The most useful boost libraries are include-only so it is handy to install just the boost headers on the select platforms (i.e. Windows) where it is cumbersome to install the compiled boost libs.

If you are using boost::test Unit Test Framework, you then need to include it directly into your test module. The penalty here is that this makes your executables bulky. On the other hand linking boost::test is easy on platforms where the compiled boost libs are easy to install, such as Debian.

We use this technique to link in boost::test on select platforms and get smaller test executables.

  1. We add this code to each test module, to control link/not linking boost::test based on an externally controllable macro “LINKTEST”:

      #define BOOST_TEST_MODULE test_name
    
      #ifdef LINKTEST
        #define BOOST_TEST_DYN_LINK
        #include <boost/test/unit_test.hpp>
      #else
        #include <boost/test/included/unit_test.hpp>
      #endif
    
      BOOST_AUTO_TEST_CASE(test_init) {
      ...
    
  2. We then add this code to the Jamfile, to link in boost::test only on linux platforms:

      project cases
        : requirements
          <target-os>linux:<define>LINKTEST
          <include>include ;
    
      ...
    

The executables when linked with boost::test will shrink by about 4,3 MB each in debug configuration, and by 0,8 MB in release.
This was tested on Debian 7.0 Wheezy 32 bit.