为什么我会和QUOT;未定义的引用"错误甚至当我有正确的头文件? [英] Why do I get "undefined reference" errors even when I include the right header files?
本文介绍了为什么我会和QUOT;未定义的引用"错误甚至当我有正确的头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我试图编译这个程序,它失败:
When I tried to compile this program, it failed:
#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;
}
它给了我以下错误:
It gave me the following errors:
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
为什么它给我的,即使我已经包括 pthreads.h中
,其中声明这些功能,这些未定义的引用错误?
Why does it give me these undefined reference errors even though I had included pthread.h
, which declares these functions?
推荐答案
您可能忘记与pthreads库链接(使用 -lpthread
在命令行上)。
You probably forgot to link with the Pthreads library (using -lpthread
on the command line).
这篇关于为什么我会和QUOT;未定义的引用&QUOT;错误甚至当我有正确的头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文