您的位置:首页 > 运维架构 > Linux

Linux下clang/gcc使用pthread.h编译出错undefined reference to `pthread_create'

2017-08-18 18:36 399 查看
最近看Unix环境高级编程,关于多线程编程的部分,按照书上的代码敲了一下,编译出错undefined reference to `pthread_create';Google了一下,是链接的时候出错,需要加参数

clang test.c -o test -lpthread

或者

g++ test.c -o test -lpthread

具体代码如下#include "apue.h"
#include<pthread.h>
#include <stdio.h>
pthread_t ntid;
void printtids(const char* s)
{
pid_t pid;
pthread_t tid;
pid=getpid();
printf("%s pid %d tid %d",s,(int)pid,(int)tid);
}
void *thr_fn(void *arg)
{
printtids("new thread:");
return ((void *)0);
}
int main()
{
int err;
err=pthread_create(&ntid,NULL,thr_fn,NULL);
if(err != 0){
printf("can't creat thread");
}
printtids("main thread:");
sleep(1);
exit(0);
}
//需要说明的一点就是,apue.h是一个自定义的头文件,因为《Unix环境高级编程》用到了这个头文件,我就去github上clone了一份,然后添加到本地,里面的内容其实很简单,除了包含一些标准库外还声明了一些函数和变量以及类型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐