title: Gtest: Undefined References
body: <p>I am trying to use GoogleTest to test a simple function, but as I run <code>make</code> in my build folder, the compiler throws <code>Undefined Reference</code> error messages at me. I've referenced the gtest header file, so I'm not sure what is wrong. Any ideas? I'm new to the entire subject of both unix and unit testing , so I could very well be missing something simple. Thanks in advance!</p>
  
  <p>Error Messages:</p>
  
  <pre><code>CMakeFiles/Proj2.dir/main.cpp.o: In function `main':
  main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
  main.cpp:(.text+0x23): undefined reference to `testing::UnitTest::GetInstance()'
  main.cpp:(.text+0x2b): undefined reference to `testing::UnitTest::Run()'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include "gtest/gtest.h"
  
  int main(int argc, char **argv) {
      ::testing::InitGoogleTest(&amp;argc, argv);
      return RUN_ALL_TESTS();
  }
  </code></pre>
  
  <p>Test.cpp</p>
  
  <pre><code>#include "gtest/gtest.h"
  #include "Testable.h"
  
  TEST(GetTwoTest, Two) {
      EXPECT_EQ(2, GetTwo());
  }
  </code></pre>
  
  <p>Testable.cpp</p>
  
  <pre><code>#include "Testable.h"
  
  int GetTwo() {
      return 3;
  }
  </code></pre>
  
  <p>Here is my CMakeLists.txt file:</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.6)
  
  SET(CMAKE_CXX_FLAGS "-std=gnu++11") #Turn on C++11 Support
  
  set(FILES_TO_TEST Testable.cpp)
  set(UNIT_TESTS Test.cpp)
  set(MAIN_FILE main.cpp)
  
  add_subdirectory(gtest) #Build all the gtest stuff
  include_directories(gtest/include)
  include_directories(.)
  add_library(codeToTest ${FILES_TO_TEST})
  
  add_executable(Proj2 ${MAIN_FILE})
  target_link_libraries(Proj2 codeToTest)
  
  add_executable(unit-test ${UNIT_TESTS})
  target_link_libraries(unit-test gtest gtest_main rt pthread codeToTest)
  </code></pre>