未定义的引用`pthread_key_create'(链接器错误) [英] undefined reference to `pthread_key_create' (linker error)
问题描述
我已从这里下载gtest 1.7.0个来源:
I have downloaded gtest 1.7.0 sources from here:
https://code.google.com/p/googletest/downloads/list
并构建gtest .a文件(lib文件)在ubuntu 13.10:
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
并调用生成的lib: libgtest.a
。在我的main.cpp文件中有:
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;
}
从我建立的终端:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lpthread -lgtest
这会产生以下错误:
/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
基于此:
错误
我也尝试过 -pthread
code> -lpthread 但给出相同的错误:
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
编辑:我也尝试指定 -pthread
作为最后一个参数:
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?
推荐答案
选项 -lgtest
正在尝试链接动态库 libgtest。因此
。您
希望链接静态库 /home/user/gtest-1.7.0/lib/.libs/libgtest.a
。
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
.
而不是:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lgtest -pthread
$ b b
use:
use:
g++ main.cpp -I/home/user/gtest-1.7.0/include /home/user/gtest-1.7.0/lib/.libs/libgtest.a -pthread
请注意您的命令行不会为生成的可执行文件提供名称,这会将
默认为 a.out
。如果你想要它调用,例如。 mytest
,然后执行:
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
这篇关于未定义的引用`pthread_key_create'(链接器错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!