C – Undefined reference to ‘pthread_key_create’ (linker error)

I downloaded the gtest 1.7.0 source code from here:

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

And build the gtest .a file (lib file) 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 generate lib: libgtest.a. In my main.cpp file:

p>

#include 
#include "gtest/gtest.h"

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

From the terminal I built:

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

This gives the following error:< /p>

/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'
/hom e/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 also tried -pthread instead -lpthread but gives the 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 also tried to specify -pthread as the last parameter:

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?

Option-lgtest is trying to link the dynamic library libgtest.so. You
want to link the static library /home/user/gtest-1.7.0/lib/.libs/libgtest.a.

Replace:

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

Please note that your command line will not provide any name for the generated executable file, this will be the default
to a.out. If you want it, for example 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

I downloaded the gtest 1.7.0 source code from here:

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

And build the gtest .a file (lib file) 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 generate lib: libgtest.a. In my main.cpp file: < /p>

#include 
#include "gtest/gt est.h"

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

From the terminal I created:

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

This gives the following error:

/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 also tried I tried -pthread instead of -lpthread but gave the same error:

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

Edit: I also tried to specify -pthread as the last parameter:

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?

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

Replace:

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

Please note that your command line does not Any name will be provided for the generated executable file, which will be the default
to a.out. If you want it, for example 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

Leave a Comment

Your email address will not be published.