FB8上无法编译多线程代码(已自已解决)
时间:2010-08-06
来源:互联网
本帖最后由 cgmeco 于 2010-08-07 01:04 编辑
复制代码
在FB8上编译时,出现以下错误,
复制代码
实在想不通,为什么会出现无法引用的问题,难道我的GCC安装不完全!求高手指导!
- #include <stdio.h>
- #include <pthread.h>
-
- void thread1(void)
- {
- int i=0;
- for(i=0;i<6;i++){
- printf("This is a pthread1.\n");
- if(i==2)
- pthread_exit(0);
- sleep(1);
- }
- }
-
- void thread2(void)
- {
- int i;
- for(i=0;i<3;i++)
- printf("This is a pthread2.\n");
- pthread_exit(0);
- }
-
- int main(void)
- {
- pthread_t id1,id2;
- int i,ret;
- ret=pthread_create(&id1,NULL,(void *) thread1,NULL);
- if(ret!=0){
- printf ("Create pthread error!\n");
- exit (1);
- }
- ret=pthread_create(&id2,NULL,(void *) thread2,NULL);
- if(ret!=0){
- printf ("Create pthread error!\n");
- exit (1);
- }
- pthread_join(id1,NULL);
- pthread_join(id2,NULL);
- exit (0);
- }
- [workstation@fse ~/programer/cprgm]$ gcc thread.c
- thread.c: In function 'main':
- thread.c:30: warning: incompatible implicit declaration of built-in function 'exit'
- thread.c:35: warning: incompatible implicit declaration of built-in function 'exit'
- thread.c:39: warning: incompatible implicit declaration of built-in function 'exit'
- /var/tmp//ccsujMHW.o(.text+0xc1): In function `main':
- : undefined reference to `pthread_create'
- /var/tmp//ccsujMHW.o(.text+0x106): In function `main':
- : undefined reference to `pthread_create'
作者: cgmeco 发布时间: 2010-08-06
在网上google才发现,由于涉及多线程的程序编译,在编译时需要链接pthread库。
所以上面的代码在编译时要做如下操作:
复制代码
所以上面的代码在编译时要做如下操作:
- gcc -o thread -lpthread thread.c
作者: cgmeco 发布时间: 2010-08-07
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28