[英]Why does this code generate the error 'undefined reference to main' upon compilation?
这是一项任务,但是现在我只是在玩我在教程中找到的代码,但是我似乎无法对其进行编译,而且我不理解收到的错误。 我的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *myfunc (void *myvar);
int main( int argc, char *argv[])
{
pthread_t thread1;
pthread_t thread2;
char *msg1 = "First thread";
char *msg2 = "Second thread";
int ret1;
int ret2;
//create threads
ret1 = pthread_create(&thread1, NULL, myfunc, (void*) msg1);
ret2 = pthread_create(&thread2, NULL, myfunc, (void*) msg2);
printf("Main function after pthread_create\n");
//join threads back to main process once completed
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Return thread ret1 = %d\n", ret1);
printf("Return thread ret2 = %d\n", ret2);
return 0;
}
void *myfunc(void *myvar)
{
char *msg;
msg = (char *) myvar;
int i;
for (i = 0; i < 10; i++) {
printf("%s%d\n", msg, i);
sleep(1);
}
return NULL;
}
我使用gcc -o c_thread c_thread.c
进行了gcc -o c_thread c_thread.c
并收到以下错误消息:
/usr/lib/gcc/x86_64-linux-gnu/5/.../.../.../x84_64-linux-gnu/ctr1.o: In function '_start':
/build/buildd/glibc-2.21/csu/.../sysdeps/x86_64/start.S:114: undefined reference to 'main'
collect2: error: ld returned 1 exit status
我知道有人问过类似的错误,但我发现的每个答案都与使用“非传统”主函数的代码有关,而我的遵循标准的int main(int argc char *argv[])
格式
任何建议将不胜感激
您在编译时缺少-lpthread
如果您使用的是IDE,请尝试通过终端进行以下编译,或者通过链接器调整设置:
gcc pro.c -o pro -lpthread
假设pro.c是您的程序文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.