线程编译问题(undefined reference to `pthread_create')
来源:互联网 发布:如何用源码建站 编辑:程序博客网 时间:2024/06/03 16:32
#include <stdio.h>#include <pthread.h>#include <stdlib.h>voidprintids(const char *s){ pid_t pid; pthread_t tid; pid = getpid(); tid = pthread_self(); printf("%s pid = [%u] tid = [%u] [0x%x]\n", s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);}void *thr_fn(void *arg){ printids("new thread:"); return ((void *)0);}intmain(){ pthread_t ntid; int err; err = pthread_create(&ntid, NULL, thr_fn, NULL); if(err != 0) printf("不能创建线程[%s]\n",strerror(err)); printids("main thread:"); sleep(3); exit(0);}
[root@localhost pthread]# gcc test.c -o test
/tmp/ccq2Mbxs.o: In function `main':
test.c:(.text+0x89): undefined reference to `pthread_create'
collect2: ld 返回 1
[root@localhost pthread]#
问题原因:
pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。
问题解决:
在编译中要加 -lpthread参数
gcc test.c -o test -lpthread
- 线程编译问题(undefined reference to `pthread_create')
- 线程编译问题(undefined reference to `pthread_create')
- 线程编译问题(undefined reference to `pthread_create')
- 线程库编译问题: undefined reference to 'pthread_create'的解决
- (转)undefined reference to 'pthread_create'问题
- undefined reference to 'pthread_create' 问题
- 线程程序编译出错 undefined reference to `pthread_create'
- apue 线程代码编译时的 undefined reference to 'pthread_create' 问题处理
- linux 创建线程问题undefined reference to 'pthread_create'
- gcc编译线程程序需带-lpthread选项(否则出错:undefined reference to `pthread_create')
- 线程异常:undefined reference to 'pthread_create' 处理
- 编译时报错:undefined reference to `pthread_create’
- 编译错误----undefined reference to 'pthread_create'问题解决
- undefined reference to 'pthread_create'
- undefined reference to 'pthread_create'
- undefined reference to 'pthread_create'
- undefined reference to `pthread_create'
- undefined reference to `pthread_create'
- 函数式编程思想:耦合和组合,第2部分
- 详解C中volatile关键字
- Tomcat下发布工程,去掉端口和工程名的方法
- 关闭linux下的selinux
- 动态对象创建(一)
- 线程编译问题(undefined reference to `pthread_create')
- iPhone 应用开发:音频播放
- 天才=99%的汗水+1%天赋
- 关闭linux下的防火墙
- 矩阵的乘法-编程心得
- Logcat中添加kernal log
- linux下配置svn error:failed to load JavaHL library
- <context-param>与<init-param>的区别与作用
- jetty嵌入式发布XFire实现的webservice