简体   繁体   中英

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标志添加到编译器。

You've not included the pthread library. Try adding

-lpthread 

to your compliation line.

You're not linking with libpthread. And manually compiling with gcc is really BFI. Put your code in a file called 'thread_test.c', and then do:

make thread_test LDFLAGS=-lpthread

Assuming no errors in your code (I didn't check), this should fix your problem...

将-pthread添加到gcc cmd行。

像这样:

   gcc -o pthread pthread.c  -lpthread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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