免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2612 | 回复: 3
打印 上一主题 下一主题

pthread_create 找不到么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-16 23:33 |只看该作者 |倒序浏览
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_t ntid;

void printids(const char *s)
{
&nbsp;&nbsp;&nbsp;&nbsp;pid_t pid;
&nbsp;&nbsp;&nbsp;&nbsp;pthread_t tid;
&nbsp;&nbsp;&nbsp;&nbsp;pid=getpid();
&nbsp;&nbsp;&nbsp;&nbsp;tid=pthread_self();
&nbsp;&nbsp;&nbsp;&nbsp;printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(unsigned int)tid,(unsigned int)tid);
}

void *thr_fn(void *arg)
{
&nbsp;&nbsp;&nbsp;&nbsp;printf("new thread: ");
&nbsp;&nbsp;&nbsp;&nbsp;return ((void *)0);
}

int main()
{
&nbsp;&nbsp;&nbsp;&nbsp;int err;
&nbsp;&nbsp;&nbsp;&nbsp;err=pthread_create(&ntid,NULL,thr_fn,NULL);
&nbsp;&nbsp;&nbsp;&nbsp;if(!err)printf("Can't create thread\n");
&nbsp;&nbsp;&nbsp;&nbsp;printids("main thread:");
&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);
&nbsp;&nbsp;&nbsp;&nbsp;exit(0);
}



gcc ./thread.c
/tmp/ccIkgqUq.o: In function `main':
thread.c.text+0x8a): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

为什么?

论坛徽章:
0
2 [报告]
发表于 2009-09-17 00:13 |只看该作者
你没有加 线程库  lpthread

论坛徽章:
0
3 [报告]
发表于 2009-09-17 09:09 |只看该作者
-lpthread

论坛徽章:
0
4 [报告]
发表于 2009-09-17 10:26 |只看该作者
gcc xxx.c -o xxx -lpthread
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP