C++ 对“pthread_key_create”的未定义引用(链接器错误)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21116622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:29:30  来源:igfitidea点击:

undefined reference to `pthread_key_create' (linker error)

c++googletest

提问by user3165964

I have downloaded gtest 1.7.0 sources from here:

我已经从这里下载了 gtest 1.7.0 源代码:

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

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

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

并在 ubuntu 13.10 上构建 gtest .a 文件(lib 文件):

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:

生成的库被称为:libgtest.a。在我的 main.cpp 文件中有:

#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

基于此: 制作 GTest 时出错

I have also tried -pthreadinstead of -lpthreadbut gives same error:

我也试过-pthread代替-lpthread但给出了同样的错误:

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 -pthreadas the last argument:

编辑:我也尝试指定-pthread为最后一个参数:

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?

同样的错误我做错了什么?

采纳答案by Mike Kinghan

The option -lgtestis 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.

该选项-lgtest正在尝试链接动态库libgtest.so。您希望链接静态库/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:

请注意,您的命令行没有为生成的可执行文件提供名称,默认为a.out. 如果你想调用它,例如mytest,然后执行:

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

回答by Mike Seymour

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

您需要在-pthread之后指定-lgtest。链接器按顺序获取库,并且只需要解析当时未定义的引用。

回答by jimi

Nope, the problem is with Gtest's build.

不,问题出在 Gtest 的构建上。

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

如果您使用标准配置方法构建它,它不会-lpthread正确提供创建libgtest.so. 因此,当您尝试构建实际使用 pthread 功能的最终共享库时,它会失败。

Instead, use the Cmake approach:

相反,使用 Cmake 方法:

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

And then manually install these into /usr/lib/

然后手动安装这些到 /usr/lib/

This version correctly links in libpthread into libgtest.

此版本将 libpthread 中的正确链接到 libgtest。

回答by parasrish

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

当您在可执行文件中使用 gtest 时,使用-pthread代替-lpthread(用于与 pthread-library 链接)。

OR

或者

Move the -lpthreadafter libgtest.a(sequence matters).

移动-lpthread之后libgtest.a(顺序很重要)。

回答by Lambage

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

要回答我们可能需要更多信息,您是否在 64 位机器上并下载了 32 位库?