【问题标题】:undefined reference to `pthread_key_create' (linker error)未定义对“pthread_key_create”的引用(链接器错误)
【发布时间】:2014-01-14 14:57:22
【问题描述】:

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

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

并在 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

生成的库被称为: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;
}

从我构建的终端:

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

基于此: error during making GTest

我也尝试过 -pthread 而不是 -lpthread 但给出了同样的错误:

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

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

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

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

【问题讨论】:

    标签: c++ googletest


    【解决方案1】:

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

    【讨论】:

    • 在clang++-3.8下,顺序不是问题,但是在g++(5.4版本)下,顺序很重要。
    • 这是该问题的唯一正确答案。令人难以置信的是,所有其他“解决方法”都获得了支持!
    • 我不知道这个...谢谢!这是正确的答案。
    【解决方案2】:

    不,问题在于 Gtest 的构建。

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

    改为使用 Cmake 方法:

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

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

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

    【讨论】:

    • 这是为我做的! :-)
    • 以及如何卸载配置错误的版本?我做了你写的,但它仍然给出同样的错误,可能仍然是由于以前的安装。
    • 此解决方案有效。在使用 Debian 10 打包 libgtest-dev 时遇到问题。在移除它并以这种方式构建之后,它变得很顺利。
    【解决方案3】:

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

    使用:

    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,然后做:

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

    【讨论】:

    • 是的,强制使用您所描述的静态 .a 库使其工作。在这里找到类似的东西:stackoverflow.com/questions/2986734/…。我还必须将 .a 文件重命名为以“lib”开头,例如:libgtest-1.7.0-linux64.a 而不是 gtest-1.7.0-linux64.a
    • 奇数。我构建的 gtest-1.7.0 不会生成像 gtest-1.7.0-linux64.a 这样的文件;我在lib/.libsdir 中得到libgtest.a。总之,很高兴问题解决了。
    • 我重命名了这些文件,因为我需要为一系列平台/拱门构建。但有点奇怪,它必须以“lib”开头,但它似乎在这里解释:stackoverflow.com/questions/6231206/lib-prefix-on-libraries
    • 如果使用动态库不行,它的用途是什么?有没有办法使用 libgtest.so 来完成这项工作?
    【解决方案4】:

    在您的可执行文件中使用 gtest 时,使用 -pthread 而不是 -lpthread(用于与 pthread-library 链接)。

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 我在原始帖子中添加了更多细节。在 ubuntu 13.10 64 位上从源代码构建 gtest。
      猜你喜欢
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2012-11-03
      相关资源
      最近更新 更多