Ubuntu编程C多线程报错--undefined reference to pthread_create

今天做多线程编译的时候报错、
在这里插入图片描述
因为pthread不是linux下的默认的库,也就是在链接的时候,无法找到phread库中哥函数的入口地址,于是链接会失败
所以我们需要链接到相应的库

gcc main.c -o test -lpthread

这样子编译就不报错能够正常运行了

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值