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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With