title: Gtest: test compiling error
body: <p>I'm trying to test a motor control lib I've wrote with googletest but I'm not been to compile the test's codes.
  The test are in a file named test.cpp such as the following:</p>
  
  <pre><code>#include &lt;gtest/gtest.h&gt;
  #include "../motor.hpp"
  TEST(constructorTest, contructorDefault)
  {
  
  }
  </code></pre>
  
  <p>And I've put a the tests main function in an other file named main.cpp.</p>
  
  <pre><code>#include &lt;gtest/gtest.h&gt;
  #include "../motor.hpp"
  int main(int argc, char* argv[])
  {
      ::testing::InitGoogleTest(&amp;argc,argv);
      RUN_ALL_TESTS();
  }
  </code></pre>
  
  <p>To compile I've excecuted the following line:</p>
  
  <pre><code>g++ main.cpp test.cpp ../motor.cpp -o test
  </code></pre>
  
  <p>The result I get is:</p>
  
  <pre><code>main.cpp:8:17: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, declared with attribute warn_unused_result [-Wunused-result]
    RUN_ALL_TESTS();
                   ^
  /tmp/ccZ5BaBH.o: In function `main':
  main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
  /tmp/ccZ5BaBH.o: In function `RUN_ALL_TESTS()':
  main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()'
  main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()'
  /tmp/ccFuAMp3.o: In function `__static_initialization_and_destruction_0(int, int)':
  test.cpp:(.text+0x5c): undefined reference to `testing::internal::GetTestTypeId()'
  test.cpp:(.text+0x84): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
  /tmp/ccFuAMp3.o: In function `constructorTest_contructorDefault_Test::constructorTest_contructorDefault_Test()':
  test.cpp:(.text._ZN38constructorTest_contructorDefault_TestC2Ev[_ZN38constructorTest_contructorDefault_TestC5Ev]+0x14): undefined reference to `testing::Test::Test()'
  /tmp/ccFuAMp3.o:(.rodata._ZTV38constructorTest_contructorDefault_Test[_ZTV38constructorTest_contructorDefault_Test]+0x20): undefined reference to `testing::Test::SetUp()'
  /tmp/ccFuAMp3.o:(.rodata._ZTV38constructorTest_contructorDefault_Test[_ZTV38constructorTest_contructorDefault_Test]+0x28): undefined reference to `testing::Test::TearDown()'
  /tmp/ccFuAMp3.o: In function `constructorTest_contructorDefault_Test::~constructorTest_contructorDefault_Test()':
  test.cpp:(.text._ZN38constructorTest_contructorDefault_TestD2Ev[_ZN38constructorTest_contructorDefault_TestD5Ev]+0x1f): undefined reference to `testing::Test::~Test()'
  /tmp/ccFuAMp3.o:(.rodata._ZTI38constructorTest_contructorDefault_Test[_ZTI38constructorTest_contructorDefault_Test]+0x10): undefined reference to `typeinfo for testing::Test'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>If I remove the test.cpp of the compiling line I get this other result:</p>
  
  <pre><code>main.cpp: In function ‘int main(int, char**)’:
  main.cpp:8:17: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, declared with attribute warn_unused_result [-Wunused-result]
    RUN_ALL_TESTS();
                   ^
  /tmp/cc61r6NU.o: In function `main':
  main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
  /tmp/cc61r6NU.o: In function `RUN_ALL_TESTS()':
  main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()'
  main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Look like What @RippeR says is right, but now I getting the following error:</p>
  
  <pre><code>/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_key_create'
  /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_getspecific'
  /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_key_delete'
  /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_setspecific'
  </code></pre>
  
  <p>Do I have to include something else?</p>
  
  <p><strong>Solution</strong>
  The problem was solve adding the -lpthread flag to compile the test.</p>