c语言中pthread_key_create未定义,链接器错误,这是怎么回事呢?

2026-04-16 22:344阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计530个文字,预计阅读时间需要3分钟。

c语言中pthread_key_create未定义,链接器错误,这是怎么回事呢?

我已从以下链接下载了gtest+1.7.0源代码:[gtest下载链接](https://code.google.com/p/googletest/downloads/list)。并在Ubuntu 13.10上成功构建了gtest的.a文件(lib文件)。

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

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

并生成lib:libgtest.a.在我的main.cpp文件中有:

c语言中pthread_key_create未定义,链接器错误,这是怎么回事呢?

#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正在尝试链接动态库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

标签:未定义

本文共计530个文字,预计阅读时间需要3分钟。

c语言中pthread_key_create未定义,链接器错误,这是怎么回事呢?

我已从以下链接下载了gtest+1.7.0源代码:[gtest下载链接](https://code.google.com/p/googletest/downloads/list)。并在Ubuntu 13.10上成功构建了gtest的.a文件(lib文件)。

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

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

并生成lib:libgtest.a.在我的main.cpp文件中有:

c语言中pthread_key_create未定义,链接器错误,这是怎么回事呢?

#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正在尝试链接动态库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

标签:未定义