我从https://computing.llnl.gov/tutorials/pthreads/开始从网上拿到以下演示
#include
#include
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!
", 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
", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d
", 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函数.有什么想法会出错吗?