关于线程编程中“undefined reference to 'pthread_create'等问题的解决
2015-01-04 14:35
513 查看
今天学习UNIX环境高级编程第11章中的线程编程,在编译第一个demo时,出现了“undefined reference to ‘pthread_create’”这种错误导致无法编译通过。
问题的原因:pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。
解决:在gcc编译的时候,命令加上 -lpthread参数即可解决。
程序11-1.c
#include "apue.h"
#include <pthread.h>
pthread_t ntid;
static void printids(const char *);
static void *func(void*);
int main(void)
{
int err;
err = pthread_create(&ntid, NULL, func, NULL);
if(err != 0)
err_quit("can't create thread: %s\n",strerror(err));
printids("main thread: ");
sleep(1);
exit(0);
}
static void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid);
}
static void *func(void *arg)
{
printids("new thread: ");
return((void*)0);
}
编译和运行:
[root@localhost 11]# gcc 11-1.c -lpthread
[root@localhost 11]# ./a.out
main thread: pid 5362 tid 3086317248 (0xb7f576c0)
new thread: pid 5362 tid 3086314384 (0xb7f56b90)
问题的原因:pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。
解决:在gcc编译的时候,命令加上 -lpthread参数即可解决。
程序11-1.c
#include "apue.h"
#include <pthread.h>
pthread_t ntid;
static void printids(const char *);
static void *func(void*);
int main(void)
{
int err;
err = pthread_create(&ntid, NULL, func, NULL);
if(err != 0)
err_quit("can't create thread: %s\n",strerror(err));
printids("main thread: ");
sleep(1);
exit(0);
}
static void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid);
}
static void *func(void *arg)
{
printids("new thread: ");
return((void*)0);
}
编译和运行:
[root@localhost 11]# gcc 11-1.c -lpthread
[root@localhost 11]# ./a.out
main thread: pid 5362 tid 3086317248 (0xb7f576c0)
new thread: pid 5362 tid 3086314384 (0xb7f56b90)
相关文章推荐
- Linux下undefined reference to ‘pthread_create’问题解决接触了Linux系统编程中的线程编程模块,可gcc sample.c(习惯把书上的sample代码写
- eclipse cdt编程: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'问题解决
- thread_join.c:(.text+0x6f): undefined reference to `pthread_create' 问题的解决
- c++多线程时 undefined reference to 'pthread_create'问题解决
- undefined reference to 'pthread_create' 问题解决
- undefined reference to 'pthread_create'问题解决
- undefined reference to 'pthread_create'问题解决
- Posix线程中undefined reference to 'pthread_create'问题解决
- 类似undefined reference to 'pthread_create'问题的解决
- undefined reference to `pthread_create'问题解决
- undefined reference to 'pthread_create'问题的解决
- [zz] undefined reference to 'pthread_create'问题解决
- QT2.2.0中多线程中遇到undefined reference to `pthread_create'问题的解决
- undefined reference to 'pthread_create'问题解决