title: Boost unit test dynamic linking on Ubuntu
body: <p>I am trying to build a unit test using Boost's unit test framework. I would like to dynamically link test suite libraries with the auto generated test module that Boost provides.  Here is the basic construction I've been using:</p>
  
  <p>test_main.cpp:</p>
  
  <pre><code>#define BOOST_TEST_DYN_LINK
  #define BOOST_TEST_MAIN
  
  #include &lt;boost/test/unit_test.hpp&gt;
  </code></pre>
  
  <p>lib_case.cpp:</p>
  
  <pre><code>#define BOOST_TEST_DYN_LINK
  #include &lt;boost/test/unit_test.hpp&gt;
  
  
  BOOST_AUTO_TEST_SUITE( test_lib )
  
  BOOST_AUTO_TEST_CASE( test_lib_case ) {
      BOOST_ASSERT(true);
  }
  BOOST_AUTO_TEST_SUITE_END()
  </code></pre>
  
  <p>Makefile:</p>
  
  <pre><code>    all: unittest unittest2 unittest3
  
      lib_case.o: lib_case.cpp
          g++ -g -c -Wall -fPIC lib_case.cpp -o lib_case.o
  
      libcase.so: lib_case.o
          g++ -shared -Wl,-soname,libcase.so -o libcase.so lib_case.o
  
      unittest: libcase.so
          g++ -o unittest test_main.cpp -L. -lcase -lboost_unit_test_framework
  
      unittest2: test_main.cpp lib_case.cpp
          g++ -o unittest2 test_main.cpp lib_case.cpp -lboost_unit_test_framework
  
      unittest3: lib_case.o
          g++ -o unittest3 test_main.cpp lib_case.o -lboost_unit_test_framework
  </code></pre>
  
  <p>Testing on Ubuntu 14.04, all executables compile and link without error.</p>
  
  <p>'unittest' fails to execute the 'test_lib' suite claiming that the setup failed, but 'unittest2' and 'unittest3' succeed:</p>
  
  <pre><code>$./unittest
  Test setup error: test tree is empty
  $./unittest2
  Running 1 test case...
  *** No errors detected
  $./unittest3
  Running 1 test case...
  *** No errors detected
  </code></pre>
  
  <p>Now for the headache: All unittest* run the test suite on Fedora 20.</p>
  
  <p>In looking at the dependency lists for 'unittest', I do see that 'libcase.so' is not listed in the Ubuntu version, but is in the Fedora 20 version. I have played around with re-ordering the dependencies, using absolute paths for the SO, and changing versions of Boost (1.54 and 1.55). Nothing worked.</p>
  
  <p>Any ideas on what may preventing the 'libcase.so' from be linked on Ubuntu 14.04 but not on Fedora 20? Am I missing some magic compiler/linker flag?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Sehe's comment and answer have helped to narrow down the problem a bit more.  If I'm understanding Boost's dynamically linked UTF implementation (at least as of 1.54/55) correctly, then the framework provides a test case manager singleton.  Every test case will automagically register with the manager upon construction.</p>
  
  <p>I think the problem is that, for whatever reason, linking on Ubuntu 'optimizes out' the static global variable used for the singleton instance of the manager during the linking of the library to the binary.  In effect, it does not link the two singleton instances despite sharing the same global static variable. It treats them as two separate instances.</p>
  
  <p>I followed the steps described in <a href="https://stackoverflow.com/questions/8623657/multiple-instances-of-singleton-across-shared-libraries-on-linux">Multiple instances of singleton across shared libraries on Linux</a> to inspect the library and binary files. Unlike in their case, the -rdynamic option does not solve my problem.</p>
  
  <p>I did some more testing and found this interesting. If you preload libcase.so object, unittest works on Ubuntu. Even though the libcase.so does not appear in its ldd listing. I feel like this is expected because the singleton for the manager was 'preloaded' when unittest runs it will link with it.</p>
  
  <pre><code>$ LD_PRELOAD=/absolute/path/to/libcase.so ./unittest
  Running 1 test case ...
  </code></pre>
  
  <p>Still have no idea why Ubuntu does not want to link as expected/intended, where Fedora does. Reading <a href="http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html" rel="nofollow noreferrer">this tutorial</a> (specifically the 'Comparison to the Microsoft DLL' section) makes me think Ubuntu is following a Windows linking pattern.</p>