5

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:

#include <gtest/gtest.h>
#include "../motor.hpp"
TEST(constructorTest, contructorDefault)
{

}

And I've put a the tests main function in an other file named main.cpp.

#include <gtest/gtest.h>
#include "../motor.hpp"
int main(int argc, char* argv[])
{
    ::testing::InitGoogleTest(&argc,argv);
    RUN_ALL_TESTS();
}

To compile I've excecuted the following line:

g++ main.cpp test.cpp ../motor.cpp -o test

The result I get is:

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

If I remove the test.cpp of the compiling line I get this other result:

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

What am I doing wrong?

EDIT

Look like What @RippeR says is right, but now I getting the following error:

/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'

Do I have to include something else?

Solution The problem was solve adding the -lpthread flag to compile the test.

tul1
  • 412
  • 1
  • 6
  • 22
  • You didn't link to Google Test. Add -lgtest to your compile command and make sure u have google test instaled on your computer. – RippeR Sep 06 '15 at 21:58

1 Answers1

7

Try:

g++ main.cpp test.cpp ../motor.cpp -o test -lgtest -lpthread

You have to link external libraries you are using. Including headers is not enough (unless library is header-only). If this solutions doesn't work, or you get error about gcc cannot find lgtest or gtest then you need to install it first (see here).

Also, note that RUN_ALL_TESTS(); returns a value, so your main() should look like:

#include <gtest/gtest.h>
#include "../motor.hpp"
int main(int argc, char* argv[])

{
    ::testing::InitGoogleTest(&argc,argv);

    return RUN_ALL_TESTS();
}

I've updated answer (check 2nd line) to cover your next problem. This is same as before, you really should start to FIND answers to your problems instead of just asking and waiting for someone to do all the work for you.

Mike S
  • 11,329
  • 6
  • 41
  • 76
RippeR
  • 1,472
  • 1
  • 12
  • 23
  • Thanks, now I have another error that I think it is related with that you said of external libs. The compiler throws the errors like this one: "undefined reference to `pthread_key_create'". – tul1 Sep 06 '15 at 22:14
  • 1
    @tul1: i've updated my answer. Note that this falls into 1st one and you should do some work by yourself, as there are already answered questions like your second part and you really should learn how to link libraries instead waiting for someone to do work for you... – RippeR Sep 06 '15 at 22:53
  • You may have (I think) to put the link information at the end to make this work. – JC1 Aug 09 '16 at 03:31
  • 3
    Note that it is best — most reliable — to put libraries at the end of the linking command line, after the object files: `g++ main.cpp test.cpp ../motor.cpp -o test -lgtest -lpthread`. I assume that what's in this answer worked on your platform; it wouldn't on Mac OS X, to name but one. – Jonathan Leffler Aug 29 '16 at 14:48