23

I have downloaded gtest 1.7.0 sources from here:

https://code.google.com/p/googletest/downloads/list

and build the gtest .a files (lib files) on ubuntu 13.10:

Linux ubuntu 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

and the resulting lib is called: libgtest.a. In my main.cpp file Have:

#include <iostream>
#include "gtest/gtest.h"

int main(){
    std::cout << "Test \n";
    int argc = 2;
    char* cp01;
    char* cp02;
    char* argv[] = {cp01, cp02};
    testing::InitGoogleTest(&argc, argv);
    return 0;
}

From a terminal I build with:

g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lpthread -lgtest

which gives the following errors:

/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status

Based on this: error during making GTest

I have also tried -pthread instead of -lpthread but gives same error:

g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -pthread -lgtest

EDIT: I have also tried to specifying -pthread as the last argument:

g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lgtest -pthread

same error What am I doing wrong?

Community
  • 1
  • 1
user3165964
  • 327
  • 1
  • 3
  • 8

5 Answers5

31

You need to specify -pthread after -lgtest. The linker takes libraries in order, and only takes as much as it needs to resolve references which are undefined at that point.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
16

Nope, the problem is with Gtest's build.

If you build it using the standard configure approach, it isn't supplying the -lpthread correctly to create libgtest.so. Hence, when you try building a final shared library that actually uses the pthread capability it fails.

Instead, use the Cmake approach:

cd gtest-1.7.0
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=ON ..
make 

And then manually install these into /usr/lib/

This version correctly links in libpthread into libgtest.

PatJ
  • 5,996
  • 1
  • 31
  • 37
jimi
  • 169
  • 1
  • 2
  • 3
    This did it for me! :-) – Patricia May 06 '15 at 21:08
  • And how to uninstall the version with the wrong configuration whatsoever? I did what you wrote but it still gives the same error, probably it's still due to the previous installation. – Schütze Nov 14 '18 at 14:02
  • This solution works. In encountered the problem with Debian 10 packaged `libgtest-dev`. After removing it and building this way it went smooth. – cen Apr 26 '20 at 11:50
9

The option -lgtest is attempting to link the dynamic library libgtest.so. You wish to link the static library /home/user/gtest-1.7.0/lib/.libs/libgtest.a.

Instead of:

g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lgtest -pthread

use:

g++ main.cpp -I/home/user/gtest-1.7.0/include /home/user/gtest-1.7.0/lib/.libs/libgtest.a -pthread

Note that your commandline supplies no name for the resulting executable, which will default to a.out. If you want it called, e.g. mytest, then do:

g++ -o mytest main.cpp -I/home/user/gtest-1.7.0/include /home/user/gtest-1.7.0/lib/.libs/libgtest.a -pthread
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • Yes forcing to use the static .a lib as you describe makes it work. Found something similar here: http://stackoverflow.com/questions/2986734/scons-how-to-force-use-of-static-library-over-shared . I also had to rename the .a file to start with "lib" like: libgtest-1.7.0-linux64.a instead of gtest-1.7.0-linux64.a – user3165964 Jan 14 '14 at 19:53
  • Odd. My build of gtest-1.7.0 generates no such file as `gtest-1.7.0-linux64.a`; I get `libgtest.a` in the `lib/.libs`dir. Anyhow, glad the prob is solved. – Mike Kinghan Jan 14 '14 at 20:42
  • I renamed the files since I need to build for a range of platforms/archs. But a bit strange that it has to start with "lib", but its seems to be explained here: http://stackoverflow.com/questions/6231206/lib-prefix-on-libraries – user3165964 Jan 14 '14 at 20:54
  • 2
    If using the dynamic lib is not ok, to what purpose does it serve? Is there a way to make this work using libgtest.so? – qed Mar 04 '14 at 16:23
5

Use -pthread instead of -lpthread (for linking with pthread-library), while you using gtest in your executable.

OR

Move the -lpthread after libgtest.a (sequence matters).

parasrish
  • 3,864
  • 26
  • 32
0

To answer we probably need more information, are you on a 64 bit machine and downloaded a 32 bit library?

Lambage
  • 367
  • 3
  • 8