Linux下clang/gcc使用pthread.h编译出错undefined reference to `pthread_create'
2017-08-18 18:36
435 查看
最近看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了一份,然后添加到本地,里面的内容其实很简单,除了包含一些标准库外还声明了一些函数和变量以及类型
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了一份,然后添加到本地,里面的内容其实很简单,除了包含一些标准库外还声明了一些函数和变量以及类型
相关文章推荐
- gcc编译线程程序需带-lpthread选项(否则出错:undefined reference to `pthread_create')
- Ubuntu下使用Eclipse编译pthread程序错误undefined reference to `pthread_create'
- 编译时报错:undefined reference to `pthread_create’
- eclipse 函数编译时报错:undefined reference to `pthread_create’
- eclipse 函数编译时报错:undefined reference to `pthread_create’
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- Linux 编译报错 -- undefined reference to 问题解决方法 (具体'pthread_create'和opencv看文章最后两条)
- linux编译出现undefined reference to symbol 'pthread_create错误解决方案
- Linux 编译报错 undefined reference to `pthread_create'
- 线程库编译问题: undefined reference to 'pthread_create'的解决
- [转]pthread_create函数编译时报错:undefined reference to 'pthread_c...
- 线程编译问题(undefined reference to `pthread_create')
- linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
- 线程编译问题(undefined reference to `pthread_create')
- gcc编译出现undefined reference to 'pthread_create'的解决方法
- 线程编译问题(undefined reference to `pthread_create')
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- apue 线程代码编译时的 undefined reference to 'pthread_create' 问题处理