c++ - 对 `pthread_key_create' 的 undefined reference ;参数顺序不能解决问题

标签 c++ linux

在 Ubuntu Linux 上,我一直在关注 gtest给出的指示 here安装gtest手动将头文件和库复制到 /usr/include/usr/lib ,分别。

然后我尝试编译以下代码( test1.cpp )

#include <gtest/gtest.h>
TEST(MathTest, TwoPlusTwoEqualsFour) {
    EXPECT_EQ(2 + 2, 4);
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest( &argc, argv );
    return RUN_ALL_TESTS();
}

使用以下命令(建议 herehere )

g++ -Wall -g -pthread test1.cpp -lgtest_main  -lgtest -lpthread  

只是为了看到以下错误:

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: undefined reference to `pthread_getspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: undefined reference to `pthread_key_delete'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status

通用答案是关于 g++ 的参数顺序。 。但是,我想我已经尝试了参数 -lpthread 的每一个组合顺序。 (有或没有 l ), -lgtest-lgtest_main 。我总是遇到同样的错误。

我不认为顺序是问题。一定是别的东西。大家有什么想法吗?

最佳答案

要将 gtest 库链接到您的 gtest,似乎必须使用静态 gtest 库(出于未知原因;请参阅 here )。因此,不要使用像这样的命令行

g++ -Wall -g -pthread test1.cpp -lgtest_main  -lgtest -lpthread

必须使用

g++ -Wall -g  -pthread test1.cpp    /usr/lib/libgtest.a 

这里,库位于 /usr/lib 中,但根据 gtest 的安装方式,文件 libgtest.a 位于其他位置。

关于c++ - 对 `pthread_key_create' 的 undefined reference ;参数顺序不能解决问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27091835/

相关文章:

c++ - 使用值类在 C++ 中实现 STL 映射

c++ - Arduino C++ 计算触发点

linux - 如何在linux系统中复制windowshare文件?

c++ - 我如何实现可以传递vector和int *的函数?

c++ - 编写 C++ 版本的代数游戏 24

c++ - 无法从 raspberry pi pico ( c sdk ) 发送串行数据

regex - 寻找匹配 `' 时意外的 EOF '

linux - linux 或 mac 终端命令可以向上滚动而不是向下滚动吗?

linux - 如何在Linux上的matlab中检查最大的连续空闲 block ?

linux - 将 flex 的命令行参数作为搜索字符串