当我试图编译这个程序时,它失败了:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *WriteNumbers(void *threadArg)
{
int start, stop;
start = atoi((char *)threadArg);
stop = start + 10;
while (start < stop)
{
printf("%d\n", start++);
sleep(1);
}
return 0;
}
int main(int argc, char **argv)
{
pthread_t thread1, thread2;
// create the threads and start the printing
pthread_create(&thread1, NULL, WriteNumbers, (void *)argv[1] );
pthread_create(&thread2, NULL, WriteNumbers, (void *)argv[2]);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
它给了我以下错误:
tmp/ccrW21s7.o: In function `main':
pthread.c:(.text+0x83): undefined reference to `pthread_create'
pthread.c:(.text+0xaa): undefined reference to `pthread_create'
pthread.c:(.text+0xbd): undefined reference to `pthread_join'
pthread.c:(.text+0xd0): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
为什么即使我包含了声明这些函数的pthread.h
,它也会给我这些未定义的引用错误?
发布于 2010-11-08 11:44:36
您可能忘记了链接pthreads库(在命令行上使用-lpthread
)。
发布于 2010-11-08 11:45:53
您需要将pthread
库链接到您的二进制文件,如下所示:
cc -o myapp myapp.c -lpthread
发布于 2016-05-14 19:26:13
做
gcc -pthread -o name filename.c (cpp)
要编译该程序,然后
./name
来运行这个程序。
https://stackoverflow.com/questions/4121090
复制相似问题