【发布时间】:2022-01-03 14:44:43
【问题描述】:
我从https://computing.llnl.gov/tutorials/pthreads/ 处从网上获取了以下演示
#include <pthread.h>
#include <stdio.h>
#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<NUM_THREADS; 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 函数。有什么想法吗?
【问题讨论】:
-
另外:根据平台,您可能需要 (a) 不同的线程编译器,(b) 不同的线程库(即
-lc_r),(c)-thread或 @ 987654328@ 或其他,代替或补充-lpthread。 -
略高于该示例,您将看到正确的编译器命令表,无论是 GCC、IBM 等。“Employed Russian”是正确的。
-
您能否取消标记我的答案,以便我删除它(并标记实际正确的答案,即投票最高的答案)?
-
编译时需要
-lpthread -
解决方案
LDFLAGS= -pthread -lpthread
标签: c linux multithreading pthreads