0

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 (as suggested here and here)

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

just to see the following 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

The generic answer was about the order of the arguments to g++. However, I think I have tried every combinatoric order of the arguments -lpthread (with and without the l), -lgtest and -lgtest_main. I always get the same errors.

I do not think the order is the problem. It must be something else. Anyone any ideas?

Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

0

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