#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)