Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gtest: test compiling error

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.

like image 550
tul1 Avatar asked Sep 06 '15 21:09

tul1


People also ask

Does Gtest run tests in parallel?

gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).

Can I use Gtest for C?

GTest is objected oriented tool and C language isn't! from GTest website gmock_for_dummies.md so you will use only macros like expect_equal, expect_bigger_than and so on... I would like to suggest you tool CMocka (or some other C unit testing tools).

How do I disable test cases in Gtest?

The docs for Google Test 1.7 suggest: If you have a broken test that you cannot fix right away, you can add the DISABLED_ prefix to its name. This will exclude it from execution. This is better than commenting out the code or using #if 0 , as disabled tests are still compiled (and thus won't rot).


1 Answers

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.

like image 103
RippeR Avatar answered Nov 10 '22 19:11

RippeR