title: CMake linking GoogleTest and UnitTests
body: <p>Im trying to setup GoogleTest to add some test on a Legacy-Code base.</p>
  
  <p>So i downloaded the latest verson of GoogleTest and extracted it side by side to the legacy code and added an simple test to verify, that the installation/configuration is correct.</p>
  
  <p>Every time i try to make the Project with CMake i end up with the following error:</p>
  
  <pre><code>Scanning dependencies of target RunUnitTests
  [ 92%] Building CXX object Testing_Code/CMakeFiles/RunUnitTests.dir/test_main.cpp.o
  Linking CXX executable RunUnitTests
  CMakeFiles/RunUnitTests.dir/test_main.cpp.o: In function `main':
  test_main.cpp:(.text+0x1b): undefined reference to `testing::InitGoogleTest(int*, char**)'
  CMakeFiles/RunUnitTests.dir/test_main.cpp.o: In function `RUN_ALL_TESTS()':
  test_main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x7): undefined reference to `testing::UnitTest::GetInstance()'
  test_main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x10): undefined reference to `testing::UnitTest::Run()'
  collect2: error: ld returned 1 exit status
  Testing_Code/CMakeFiles/RunUnitTests.dir/build.make:85: recipe for target 'Testing_Code/RunUnitTests' failed
  make[2]: *** [Testing_Code/RunUnitTests] Error 1
  CMakeFiles/Makefile2:429: recipe for target 'Testing_Code/CMakeFiles/RunUnitTests.dir/all' failed
  make[1]: *** [Testing_Code/CMakeFiles/RunUnitTests.dir/all] Error 2
  Makefile:116: recipe for target 'all' failed
  make: *** [all] Error 2
  </code></pre>
  
  <p>(full output visible <a href="https://gist.github.com/cedi/9203d899f66e367757c5" rel="nofollow">here</a>)</p>
  
  <p>Iv'e been searching for a fix for a long time. I have googled and read so many links at the internet that i lost the overview.
  Im really new to CMake and think i miss something small but major...</p>
  
  <p>My folder-hierarchy looks like the following:</p>
  
  <pre><code>+
  |-+CMake
  | |-run.sh
  | |-BuildAll.sh
  | |-CMakeLists.txt
  |
  |-+cpp-project
  | |-CMakeLists.txt
  | |-+work
  | | |-CMakeLists.txt
  | | 
  | |-+include
  |   |-CMakeLists.txt
  |
  |-+GoogleTest
  | |-CMakeLists.txt
  | |-+googlemock
  | |-+googletest
  | |-RADME.md
  | |-travis.sh
  |
  |-+Testing_Code
    |-CMakeLists.txt
    |-+test
      |-CMakeLists.txt
      |-test.
  </code></pre>
  
  <p>To start the compiling i run <code>sh ~/code/CMake/run.sh</code> which looks like the following:</p>
  
  <pre><code>cp BuildAll.sh ./../BuildAll.sh
  cp CMakeLists.txt ./../CMakeLists.txt
  cd ..
  sh BuildAll.sh $1
  </code></pre>
  
  <p>And BuildAll does some cleaning-stuff and generate some folders, etc.:</p>
  
  <pre><code>#!/bin/bash
  
  alwaysCleanBuild=$1
  control="control"
  unittest_out="$control/test"
  build="build"
  include="$build/include"
  
  if [ $alwaysCleanBuild = true ]; then
      if [ -d "$control" ]; then
          rm -r "$control"
      fi
  
      if [ -d "$build" ]; then
          rm -r "$build"
      fi
  fi
  
  mkdir $control
  mkdir $build
  mkdir $include
  
  cd $build
  
  cp -r ../GoogleTest/googletest/include/* ./include/
  cp ../cpp-project/include/* ./include
  
  cmake ..
  
  if [ $? = 0 ]; then
      make
  
      if [ $? = 0 ]; then
              ./../control/RunUnitTests
      fi
  fi
  </code></pre>
  
  <p>New Folders in short:</p>
  
  <ul>
  <li><code>control</code> is the output dir of the cpp-project libs/binarys/what ever you like to call the output ;)</li>
  <li><code>build</code> is the CMake destination for generating the Makefile, etc.</li>
  <li><code>build\include</code> is the public include-dir for the tests to access gtest and cpp-projects headders.</li>
  </ul>
  
  <p>The CMake/CMakeLists.txt is copyed from the <code>BuildAll.sh</code> Script to the root-directory. So there is a 'root'-<code>CMakeLists.txt</code>. This CMakeLists specifies the <code>include_directories</code> and the output-directory('s):</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.2)
  
  project(Test)
  
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../control)
  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../control)
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../control)
  
  include_directories(${CMAKE_BINARY_DIR}/include)
  
  add_subdirectory(cpp-project)
  add_subdirectory(GoogleTest)
  add_subdirectory(Testing_Code)
  </code></pre>
  
  <p>GoogleTest's CMakeLists are all untouched and they are running like a charm.
  The cpp-projects CMakeFile are running very fine too and they output everything to the <code>control</code> directory. (I'm really satisfied with CMake. It's much quicker and simpler than the plain old Makefiles (in my oppinion) for the 'easy-beginner-steps').</p>
  
  <p>The CMakeLists from <code>/Testing_Code/</code> also looks verry simple, but i think in there you will find my mistake:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.2)
  
  project(Testing)
  
  # Create main.cpp which uses gtest
  file(WRITE test_main.cpp "#include &lt;gtest/gtest.h&gt;\n\n")
  file(APPEND test_main.cpp "int main(int argc, char **argv)\n{\n")
  file(APPEND test_main.cpp "    testing::InitGoogleTest(&amp;argc, argv);\n")
  file(APPEND test_main.cpp "    return RUN_ALL_TESTS();\n")
  file(APPEND test_main.cpp "}\n")
  
  # build all tests
  ADD_SUBDIRECTORY(test)
  # ADD_SUBDIRECTORY(test_2)
  # ADD_SUBDIRECTORY(test_3)
  
  # build the test_main
  ADD_EXECUTABLE(RunUnitTests test_main.cpp ${GTEST_INCLUDE})
  
  # Link RunUnitTests with GoogleTest and cpp-project
  TARGET_LINK_LIBRARIES(RunUnitTests ${GoogleTest} ${cpp-project})
  </code></pre>
  
  <p>I can't find the mistake in here and i really hope that someone could help me with this.</p>