12.10在链接pthread.a的时候报错

软件和网站开发以及相关技术探讨
回复
star385
帖子: 4
注册时间: 2012-11-11 19:14

12.10在链接pthread.a的时候报错

#1

帖子 star385 » 2013-04-22 16:42

大家好,最近在学习c语言,按照别人的例子,写了一个创建线程,编译链接的时候,发现一个问题,源码如下:

代码: 全选

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.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(arg);
	return NULL;
}

int main(void)
{
	int err;

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

	return 0;
}
用gcc -o pthread -lpthread pthreadtest.c进行编译链接,编译是可以通过的,链接的时候出错,出错信息如下:
/tmp/ccXYMnX0.o:在函数‘main’中:
testthread1.c:(.text+0x83):对‘pthread_create’未定义的引用
collect2: 错误: ld 返回 1

在9.10下是可以编译链接通过的,大家知道为什么吗?
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

Re: 12.10在链接pthread.a的时候报错

#2

帖子 YeLee » 2013-04-22 17:33

◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
star385
帖子: 4
注册时间: 2012-11-11 19:14

Re: 12.10在链接pthread.a的时候报错

#3

帖子 star385 » 2013-04-24 16:30

亲,按照您的方式编译链接通过,非常感谢!!
回复