3

On Ubuntu Linux, I have been following the gtest instructions given here to install gtest with manually copying the header files and libraries to /usr/include and /usr/lib, respectively.

I then tried to compile the following code (test1.cpp)

#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();
}

with the following command

g++ -lgtest -lgtest_main -lpthread test1.cpp 

just to see yet another unhelpful error message:

/usr/bin/ld: /tmp/ccQlmghI.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

How can I fix this? Or is there another unittest framework which can be used in a SIMPLE way, with a working example?

The same question has been asked here, but without an answer.

Alex
  • 41,580
  • 88
  • 260
  • 469

2 Answers2

8

Order of arguments to g++ matters a lot. Source files should come before object files and libraries (from high-level to low-level libraries), etc. Read the chapter on invoking GCC.

So try:

g++ -Wall -g -pthread test1.cpp -lgtest_main  -lgtest -lpthread 
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Creates a bunch of new errors:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: undefined reference to `pthread_key_create' /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: undefined reference to `pthread_getspecific' /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: undefined reference to `pthread_key_delete' /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: undefined reference to `pthread_setspecific' collect2: error: ld returned 1 exit status – Alex Nov 23 '14 at 16:49
  • Maybe pthread is not installed or something? – Alex Nov 23 '14 at 16:50
  • What does `-pthread` in your example mean? – Alex Nov 23 '14 at 16:51
  • 1
    -pthread tells gcc to use pthread libs, no need to use -lpthread – Galik Nov 23 '14 at 16:52
  • So you can also write just `-gtest_main` instead of `-lgtest_main` then...? – Alex Nov 23 '14 at 16:53
  • 1
    No, -pthread is a built in switch for GCC not a new way to link libs in general – Galik Nov 23 '14 at 16:53
  • Please read the documentation about invoking GCC... I linked it in my answer. – Basile Starynkevitch Nov 23 '14 at 16:54
  • Still, I get a bunch of errors. Your answer only changed the content of the errors so far... – Alex Nov 23 '14 at 16:56
  • Should I start a new question to try to get an answer to my new problem? – Alex Nov 23 '14 at 17:00
  • I believe you should take several hours to read documentation (about GCC, Gtest, autotools, etc...) – Basile Starynkevitch Nov 23 '14 at 17:09
1

For linking the gtest library to your gtest, it seems one must use the static gtest libraries (for unknown reasons; see here). So instead of using the command line like

g++ -Wall -g -pthread test1.cpp -lgtest_main  -lgtest -lpthread

one must use

g++ -Wall -g  -pthread test1.cpp    /usr/lib/libgtest.a 

Here, the library is located in /usr/lib, but depending on how gtest was installed the file libgtest.a is located somethere else.

Alex
  • 41,580
  • 88
  • 260
  • 469