title: Linking project source code with boost tests under CMake
body: <p>I am trying to find the best way to link my project source code with my boost unit tests. I have a fairly basic project setup right now using CMake, however all examples of boost UTF that I have come across have shown very basic tests which do not touch source code in a project that is along side the tests.</p>
  
  <p>As a minimal example I have the following:</p>
  
  <p>CMakeLists.txt</p>
  
  <pre><code>cmake_minimum_required(version 2.8) 
  project(test-project) 
  find_package(Boost 1.55 REQUIRED COMPONENTS unit_test_framework )
  include_directories(${Boost_INCLUDE_DIRS})
  link_directories(${Boost_LIBRARY_DIRS})
  
  add_subdirectory(src) 
  enable_testing()
  add_subdirectory(test) 
  </code></pre>
  
  <p>src/CMakeLists.txt  </p>
  
  <pre><code>add_executable(example main.cpp foo.cpp)  
  </code></pre>
  
  <p>src/foo.h</p>
  
  <pre><code>#include &lt;string&gt;
  std::string hello(std::string name);
  </code></pre>
  
  <p>src/foo.cpp</p>
  
  <pre><code>#include "foo.h"
  std::string hello(std::string name) { return "Hello " + name; }
  </code></pre>
  
  <p>src/main.cpp - Uses foo in a simple way</p>
  
  <p>test/CMakeLists.txt</p>
  
  <pre><code>include_directories (../src) 
  set(TEST_REQUIRED_SOURCES ../src/foo.cpp)
  
  add_executable (test test.cpp ${TEST_REQUIRED_SOURCES}) 
  target_link_libraries(test ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
  
  add_test(SimpleTest test)
  </code></pre>
  
  <p>test/test.cpp</p>
  
  <pre><code>#define BOOST_TEST_DYN_LINK
  #define BOOST_TEST_MAIN
  #define BOOST_TEST_MODULE SimpleTest
  #include &lt;boost/test/unit_test.hpp&gt;
  
  #include "foo.h"
  
  BOOST_AUTO_TEST_CASE(ShouldPass) {
      BOOST_CHECK_EQUAL(hello("fred"), "Hello fred")
  }
  </code></pre>
  
  <p>While this works, I'd like to avoid the following:</p>
  
  <ol>
  <li>Defining TEST_REQUIRED_SOURCES with a list of all of my files required to compile.</li>
  <li>Avoiding the duplicate compilation of code.</li>
  </ol>
  
  <p>Does my structure look correct for this sort of project? Would it make sense to compile my code under src into a library instead? Most of my experience with testing is from C# where this is much simpler.</p>