Link to home
Start Free TrialLog in
Avatar of vijay_rangaraj
vijay_rangaraj

asked on

C Multithreading

Please look at the following code: It simply creates a thread.

#include <stdio.h>
#include <pthread.h>

void* thr(void*);

main()
{
   pthread_t thread1;
  if(pthread_create(&thread1,NULL,thr,NULL)==0)
   printf("error creating thead");
 sleep(2);
 
}

void * thr(void *arg)
{
   printf("Hi from thread");
}

    This code, when compiled, makes the following complaint:

/tmp/ccq5LR1c.o: In function 'main':
/tmp/ccq5LR1c.o(.text+0x14): undefined reference to 'pthread_create'

   I use linux kernel 2.2(caldera), and type 'cc' to compile the program. Please tell me what to do.
ASKER CERTIFIED SOLUTION
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mnashadka
mnashadka

I agree that it's a linker problem, but on most compilers you're supposed to use the -pthread option (rather than -lpthread) since it sometimes defines other macros to use reentrant functions or whatever (cc -o my_program my_program.c -pthread).  Good luck.
Avatar of vijay_rangaraj

ASKER

Good work,friedrich and mnashadka, both your suggestions worked for me, (ie the programs compiled correctly both with -pthread and -lpthread). Thanks again.

Bye.