繁体   English   中英

请帮助编译该程序

[英]please help in compiling this program

#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);
 }
}

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;
}

gcc -o pthread pthread.c 
/tmp/cckJD3rd.o: In function `main':
pthread.c:(.text+0x7a): undefined reference to `pthread_create'
pthread.c:(.text+0xa2): undefined reference to `pthread_create'
pthread.c:(.text+0xb6): undefined reference to `pthread_join'
pthread.c:(.text+0xca): undefined reference to `pthread_join'
collect2: ld returned 1 exit status

您需要将-pthread标志添加到编译器。

您尚未包括pthread库。 尝试添加

-lpthread 

到你的补充线。

您没有与libpthread链接。 用gcc手动编译确实是BFI。 将您的代码放在一个名为“ thread_test.c”的文件中,然后执行以下操作:

使thread_test LDFLAGS = -lpthread

假设您的代码没有错误(我没有检查),这应该可以解决您的问题...

将-pthread添加到gcc cmd行。

像这样:

   gcc -o pthread pthread.c  -lpthread

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM