kot
kot

Reputation: 65

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

Upvotes: 0

Views: 642

Answers (5)

Whoami
Whoami

Reputation: 14428

like this:

   gcc -o pthread pthread.c  -lpthread

Upvotes: 0

mile davidovicc
mile davidovicc

Reputation: 29

Add -pthread to gcc cmd line.

Upvotes: 0

evadeflow
evadeflow

Reputation: 4944

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...

Upvotes: 1

The Archetypal Paul
The Archetypal Paul

Reputation: 41769

You've not included the pthread library. Try adding

-lpthread 

to your compliation line.

Upvotes: 5

Bill Lynch
Bill Lynch

Reputation: 81996

You need to add the -pthread flag to your compiler.

Upvotes: 8

Related Questions