6

I'm learning to google test. I downloaded gtest, ran commands ./configure and make and ended with

$ sudo cp -a include/gtest /usr/include
$ sudo cp -a lib/.libs/* /usr/lib/

i got all this from here. I tried to run this code

#include <gtest/gtest.h>
TEST(MathTest, TwoPlusTwoEqualsFour) {
    EXPECT_EQ(2 + 2, 4);
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest( &argc, argv );
    return RUN_ALL_TESTS();
}

like this

 $ export GTEST_HOME=~/usr/gtest
 $ export LD_LIBRARY_PATH=$GTEST_HOME/lib:$LD_LIBRARY_PATH
 $ g++ -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread test.cpp

but i received an error

/usr/bin/ld: /tmp/ccVTj3Rk.o: undefined reference to symbol '_ZN7testing8internal9EqFailureEPKcS2_RKSsS4_b'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Am i doing something wrong or is it a bug?

Community
  • 1
  • 1
jabk
  • 1,388
  • 4
  • 25
  • 43
  • AFAIK, you should not implement a `main()` if you link against `gtest_main` – tgmath Oct 07 '14 at 09:17
  • so I should delete `main()`? What do you purpose – jabk Oct 07 '14 at 09:21
  • 1
    I tested it without the `main()` and compiled it like you did. The important thing is to add the libs after the `test.cpp`. – tgmath Oct 07 '14 at 09:34
  • Please also see https://stackoverflow.com/questions/27091412/how-to-compile-a-gtest-cpp-file#27091541 – Alex Nov 23 '14 at 16:51
  • 1
    I fixed this changing the position of test.cpp on the commandline. Just place it before the libraries. i.e: g++ test.cpp -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread – user9869932 Jun 11 '15 at 16:10

2 Answers2

7

There are some errors in your setup.

You copied your include/gtest into /usr/include (sudo cp -a include/gtest /usr/include), but when you try to compile you tell the compiler to look for the gtest headers in the ~/usr/gtest directory, not in the directory you set up before (/usr/include/gtest). The same things happed with the lib/.libs/* files. You copy them in a place and at compile time you ask for them in another place.

Keeping the following configurations:

$ sudo cp -a include/gtest /usr/include
$ sudo cp -a lib/.libs/* /usr/lib/

change #include <gtest/gtest.h> into #include "gtest/gtest.h" and try to compile like this:

g++ -g -Wall <your .cpp files> -I /usr/include/gtest/ -L /usr/lib/ -lgtest -lgtest_main -lpthread

And you should see the following output after running the ./a.out file (or whatever name you assign to the executable file)

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MathTest
[ RUN      ] MathTest.TwoPlusTwoEqualsFour
[       OK ] MathTest.TwoPlusTwoEqualsFour (0 ms)
[----------] 1 test from MathTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.
Roxana Istrate
  • 632
  • 1
  • 6
  • 18
0

Put the *.cpp source file in the front of library linked options:

g++ -I /home/gtest/googletest/include/ -L /home/gtest/googletest/ gtest_demo.cpp -lgtest -lgtest_main -lpthread
shmsr
  • 3,802
  • 2
  • 18
  • 29
jason
  • 1