linux如何定义一个非空指针,在Linux中对pthread_create的未定义引用

#include

#include

#define NUM_THREADS 5

void *PrintHello(void *threadid)

{

long tid;

tid = (long)threadid;

printf("Hello World! It's me, thread #%ld!\n", tid);

pthread_exit(NULL);

}

int main (int argc, char *argv[])

{

pthread_t threads[NUM_THREADS];

int rc;

long t;

for(t=0; t

printf("In main: creating thread %ld\n", t);

rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);

if (rc){

printf("ERROR; return code from pthread_create() is %d\n", rc);

exit(-1);

}

}

pthread_exit(NULL);

}

但是,当我在运行Ubuntu Linux 9.04的计算机上对其进行编译时,出现以下错误:

corey@ubuntu:~/demo$ gcc -o term term.c

term.c: In function ‘main’:

term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’

/tmp/cc8BMzwx.o: In function `main':

term.c:(.text+0x82): undefined reference to `pthread_create'

collect2: ld returned 1 exit status

这对我来说没有任何意义,因为标头包含pthread.h ,应具有pthread_create函数。 任何想法出什么事了吗?

#1楼

您只需要在proprieties => C / C ++ build => GCC C ++ Linker => Libraries =>顶部“ Libraries(-l)”中添加“ pthread”。 而已

#2楼

日食

属性-> c / c ++ Build->设置-> GCC C ++链接器->顶部的库中添加“ pthread”

#3楼

在Anjuta中,转到“生成”菜单,然后“配置项目”。 在“配置选项”框中,添加:

LDFLAGS='-lpthread'

希望它也能帮助别人...

#4楼

到目前为止,这个问题的两个答案都是错误的 。

对于Linux,正确的命令是:

gcc -pthread -o term term.c

通常,库应该在命令行中跟随源和对象,并且-lpthread不是“选项”,它是库规范。 在仅安装libpthread.a系统上,

gcc -lpthread ...

将无法链接。

#5楼

有时,如果您使用多个库,请检查库依赖性。 (例如,-lpthread -lSDL ... <==> ... -lSDL -lpthread)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值