簡體   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