解决undefined reference to `pthread_atfork'/`pthread_detach'/`pthread_create'/`pthread_join'

在编译Muduo库的代码时遇到链接错误,指出缺少pthread_atfork、pthread_detach、pthread_create和pthread_join的引用。通过在编译时添加-pthread参数而非-lpthread解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在编译陈硕github上面的代码时,遇到了链接错误:

../Thread.o: In function `(anonymous namespace)::ThreadNameInitializer::ThreadNameInitializer()':

Thread.cpp:(.text+0x5f): undefined reference to `pthread_atfork'
../Thread.o: In function `muduo::Thread::~Thread()':
Thread.cpp:(.text+0x3f9): undefined reference to `pthread_detach'
../Thread.o: In function `muduo::Thread::start()':
Thread.cpp:(.text+0x50e): undefined reference to `pthread_create'
../Thread.o: In function `muduo::Thread::join()':
Thread.cpp:(.text+0x5ce): undefined reference to `pthread_join'

collect2: error: ld returned 1 exit status


在编译时已经加了-lpthread,还是有这个问题。

在这里找到了答案:http://stackoverflow.com/questions/2373109/what-library-to-be-to-be-used-to-avoid-undefined-reference-to-pthread-atfork

pthread_atfork是POSIX标准,在编译时要加上-pthread

-lpthread是老版本的gcc编译器用的,在新版本中应该用-pthread取代-lpthread

ubuntu@raspberrypi:~$ cat ~/PX4-Autopilot/build/px4_sitl_default/build_gazebo-classic/CMakeFiles/CMakeError.log Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output: Change Dir: /home/ubuntu/PX4-Autopilot/build/px4_sitl_default/build_gazebo-classic/CMakeFiles/CMakeTmp Run Build Command(s):/bin/ninja cmTC_778f9 && [1/2] Building C object CMakeFiles/cmTC_778f9.dir/src.c.o [2/2] Linking C executable cmTC_778f9 FAILED: cmTC_778f9 : && /bin/cc -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_778f9.dir/src.c.o -o cmTC_778f9 && : /bin/ld: CMakeFiles/cmTC_778f9.dir/src.c.o: in function `main': src.c:(.text+0x48): undefined reference to `pthread_create' /bin/ld: src.c:(.text+0x50): undefined reference to `pthread_detach' /bin/ld: src.c:(.text+0x5c): undefined reference to `pthread_join' collect2: error: ld returned 1 exit status ninja: build stopped: subcommand failed. Source file was: #include <pthread.h> void* test_func(void* data) { return data; } int main(void) { pthread_t thread; pthread_create(&thread, NULL, test_func, NULL); pthread_detach(thread); pthread_join(thread, NULL); pthread_atfork(NULL, NULL, NULL); pthread_exit(NULL); return 0; } Determining if the function pthread_create exists in the pthreads failed with the following output: Change Dir: /home/ubuntu/PX4-Autopilot/build/px4_sitl_default/build_gazebo-classic/CMakeFiles/CMakeTmp Run Build Command(s):/bin/ninja cmTC_1816c && [1/2] Building C object CMakeFiles/cmTC_1816c.dir/CheckFunctionExists.c.o [2/2] Linking C executable cmTC_1816c FAILED: cmTC_1816c : && /bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_1816c.dir/CheckFunctionExists.c.o -o cmTC_1816c -lpthreads && : /bin/ld: cannot find -lpthreads collect2: error: ld returned 1 exit status ninja: build stopped: subcommand failed.
最新发布
08-10
wang@ubuntu:~$ cd ~/dog_ws wang@ubuntu:~/dog_ws$ cat build/CMakeFiles/CMakeError.log Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output: Change Dir: /home/wang/dog_ws/build/CMakeFiles/CMakeTmp Run Build Command(s):/usr/bin/make cmTC_c5a31/fast && /usr/bin/make -f CMakeFiles/cmTC_c5a31.dir/build.make CMakeFiles/cmTC_c5a31.dir/build make[1]: 进入目录“/home/wang/dog_ws/build/CMakeFiles/CMakeTmp” Building C object CMakeFiles/cmTC_c5a31.dir/src.c.o /usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_c5a31.dir/src.c.o -c /home/wang/dog_ws/build/CMakeFiles/CMakeTmp/src.c Linking C executable cmTC_c5a31 /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_c5a31.dir/link.txt --verbose=1 /usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD -rdynamic CMakeFiles/cmTC_c5a31.dir/src.c.o -o cmTC_c5a31 /usr/bin/ld: CMakeFiles/cmTC_c5a31.dir/src.c.o: in function `main': src.c:(.text+0x46): undefined reference to `pthread_create' /usr/bin/ld: src.c:(.text+0x52): undefined reference to `pthread_detach' /usr/bin/ld: src.c:(.text+0x63): undefined reference to `pthread_join' collect2: error: ld returned 1 exit status make[1]: *** [CMakeFiles/cmTC_c5a31.dir/build.make:87:cmTC_c5a31] 错误 1 make[1]: 离开目录“/home/wang/dog_ws/build/CMakeFiles/CMakeTmp” make: *** [Makefile:121:cmTC_c5a31/fast] 错误 2 Source file was: #include <pthread.h> void* test_func(void* data) { return data; } int main(void) { pthread_t thread; pthread_create(&thread, NULL, test_func, NULL); pthread_detach(thread); pthread_join(thread, NULL); pthread_atfork(NULL, NULL, NULL); pthread_exit(NULL); return 0; } Determining if the function pthread_create exists in the pthreads failed with the following output: Change Dir: /home/wang/dog_ws/build/CMakeFiles/CMakeTmp Run Build Command(s):/usr/bin/make cmTC_5aede/fast && /usr/bin/make -f CMakeFiles/cmTC_5aede.dir/build.make CMakeFiles/cmTC_5aede.dir/build make[1]: 进入目录“/home/wang/dog_ws/build/CMakeFiles/CMakeTmp” Building C object CMakeFiles/cmTC_5aede.dir/CheckFunctionExists.c.o /usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTC_5aede.dir/CheckFunctionExists.c.o -c /usr/share/cmake-3.16/Modules/CheckFunctionExists.c Linking C executable cmTC_5aede /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_5aede.dir/link.txt --verbose=1 /usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -rdynamic CMakeFiles/cmTC_5aede.dir/CheckFunctionExists.c.o -o cmTC_5aede -lpthreads /usr/bin/ld: 找不到 -lpthreads collect2: error: ld returned 1 exit status make[1]: *** [CMakeFiles/cmTC_5aede.dir/build.make:87:cmTC_5aede] 错误 1 make[1]: 离开目录“/home/wang/dog_ws/build/CMakeFiles/CMakeTmp” make: *** [Makefile:121:cmTC_5aede/fast] 错误 2
07-02
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值