• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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.
0
vijay_rangaraj
Asked:
vijay_rangaraj
1 Solution
 
fridomCommented:
It's  a linker problem you do not have linke against libphtread I guess. Check if you have it installed, if not install it than add it to the compile command line
cc -o my_program my_program.c -lphtread

(assuming it is installed in a path where the linker looks int for libraries if  that is not the case use the -L flag to add the dirctory to that library)

See two the C programming FAQ

Regards
Friedrich
0
 
mnashadkaCommented:
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.
0
 
vijay_rangarajAuthor Commented:
Good work,friedrich and mnashadka, both your suggestions worked for me, (ie the programs compiled correctly both with -pthread and -lpthread). Thanks again.

Bye.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now