title: Boost Test with CMake - undefined main
body: <p>I'm having trouble building a little program that uses Boost.Test on my Mac with a Boost installed by MacPorts at <code>/opt/local/lib/</code></p>
  
  <p>Here's my minimal source file, <code>test.cpp</code>:</p>
  
  <pre><code>#define BOOST_TEST_MODULE MyTest
  #include &lt;boost/test/unit_test.hpp&gt;
  
  BOOST_AUTO_TEST_CASE(test1) {
  }
  </code></pre>
  
  <p>and my <code>CMakeLists.txt</code>:</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.6)
  project (test)
  find_package(Boost COMPONENTS unit_test_framework REQUIRED)
  add_executable(test test.cpp)
  </code></pre>
  
  <p>and an excerpt of from <code>make VERBOSE=1</code>:</p>
  
  <pre><code>[100%] Building CXX object CMakeFiles/test.dir/test.cpp.o
  g++ -o CMakeFiles/test.dir/test.cpp.o -c /Users/exclipy/Code/cpp/inline_variant/question/test.cpp
  Linking CXX executable test
  "/Applications/CMake 2.8-5.app/Contents/bin/cmake" -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
  g++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/test.dir/test.cpp.o-o test
  Undefined symbols for architecture x86_64:
    "_main", referenced from:
        start in crt1.10.6.o
    "vtable for boost::unit_test::unit_test_log_t", referenced from:
        boost::unit_test::unit_test_log_t::unit_test_log_t() in test.cpp.o
        boost::unit_test::unit_test_log_t::~unit_test_log_t() in test.cpp.o
    NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  </code></pre>
  
  <p>As you can see, it doesn't know how to link to Boost library.  So I try adding to CMakeLists.txt:</p>
  
  <pre><code>target_link_libraries(test boost_unit_test_framework)
  </code></pre>
  
  <p>But I just get:</p>
  
  <pre><code>g++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/test.dir/test.cpp.o-o test -lboost_unit_test_framework 
  ld: library not found for -lboost_unit_test_framework
  </code></pre>
  
  <p>From lots of trial and error, I've found that manually running this works:</p>
  
  <pre><code>$ g++ test.cpp -L/opt/local/lib -lboost_unit_test_framework -DBOOST_TEST_DYN_LINK
  </code></pre>
  
  <p>But after hours of fiddling, I can't get it to build from CMake.  I don't care whether it links dynamically or statically, I just want it to work.</p>