gpt4 book ai didi

c++ - 未定义对 `pthread_key_create' 的引用(链接器错误)

转载 作者:可可西里 更新时间:2023-11-01 14:52:55 27 4
gpt4 key购买 nike

我已经从这里下载了 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

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

最佳答案

需要在-lgtest后指定-pthread。链接器按顺序获取库,并且只获取解析当时 undefined reference 所需的数量。

关于c++ - 未定义对 `pthread_key_create' 的引用(链接器错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21116622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com