Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

c++ - undefined reference to `pthread_key_create' (linker error)

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 
";
    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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...