免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 40896 | 回复: 5

明明include<pthread.h>,为什么 undefined reference to `pthread_create' [复制链接]

论坛徽章:
0
发表于 2006-08-19 17:34 |显示全部楼层
程序如下:
#include "apue.h"
#include <pthread.h>

pthread_t ntid;

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);
}

void *
thr_fn(void *arg)
{
        printids("new thread: ";
        return((void *)0);
}

int
main(void)
{
        int                err;

        err = pthread_create(&ntid, NULL, thr_fn, NULL);
        if (err != 0)
                err_quit("can't create thread: %s\n", strerror(err));
        printids("main thread:";
        sleep(1);
        exit(0);
}

结果:
/tmp/cckp7gaI.o(.text+0x70): In function `main':
: undefined reference to `pthread_create'
/tmp/cckp7gaI.o(.text+0x9: In function `main':
: undefined reference to `err_quit'
collect2: ld returned 1 exit status

论坛徽章:
0
发表于 2006-08-19 18:33 |显示全部楼层
加编译参数-lpthread

论坛徽章:
0
发表于 2006-08-19 19:18 |显示全部楼层
3X czk
pthread_create通过了
但是err_quit还是找不到啊

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2006-08-19 20:10 |显示全部楼层
err_quit 是作者自己定义的函数,封装了perror,strerror,fprintf这样的函数.

论坛徽章:
0
发表于 2006-08-19 20:44 |显示全部楼层
这是<unix环境高级编程>里的程序吧,前几天我也遇到过.
里面包含了一些库函数的定义.
这里有关于它的一篇文章.
http://www.yesky.com/3/88003.shtml
你可以用系统调用需要的头文件来替代#include "apue.h".
也可以到网上下载#include "apue.h".的原代码.
然后用CC -I<incdir> main.cpp(其中incdir是"ourhdr.h"所在的路径)。

论坛徽章:
0
发表于 2006-08-21 11:14 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP