UNIX环境高级编程中的11章程序11-2编译问题---undefined reference to `pthread_create'
2015-03-10 21:37
591 查看
#include<apue.h> #include<pthread.h> pthread_t ntid; void printids(const char *s) { pid_t pid; pthread_t tid; pid=getpid(); tid=pthread_self(); printf("%s pid %lu tid %lu (0x%lx)\n",s,(unsigned long)pid,(unsigned long)tid,(unsigned long)tid); } void * thr_fn(void *arg) { printids("new thread: "); return((void *)0); } int main(void) { int err; err=pthread_create(&ntid,NULL,thr_fn,NULL); if(err!=0) err_exit(err,"can't create thread"); printids("main thread:"); sleep(1); exit(0); }
编译这段代码时出现上述问题原因:
pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。
解决方法:在编译中要加 -lpthread选项,类似于在使用<math.h>是的选项 -lm.
编译:gcc -o program11-2 program11-2.c -lapue -lpthread
相关文章推荐
- unix环境高级编程-undefined reference to 'pthread_create'问题解决
- 编译错误----undefined reference to 'pthread_create'问题解决
- gcc编译线程程序需带-lpthread选项(否则出错:undefined reference to `pthread_create')
- apue 线程代码编译时的 undefined reference to 'pthread_create' 问题处理
- Linux 编译报错 -- undefined reference to 问题解决方法 (具体'pthread_create'和opencv看文章最后两条)
- 线程编译问题(undefined reference to `pthread_create')
- 线程编译问题(undefined reference to `pthread_create')
- linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
- 解决Unix环境高级编程示例中的 undefined reference to `err_quit'
- 线程库编译问题: undefined reference to 'pthread_create'的解决
- undefined reference to 'pthread_create'问题解决
- ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题
- [转]pthread_create函数编译时报错:undefined reference to 'pthread_c...
- 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' 问题解决