0

Possible Duplicate:
undefined reference to pthread_create in linux (c programming)

I am trying to implement Thread chain in Ubuntu in C. When I compile the following code, I get the errors of Undefined reference to these thread library function even though I have added the header file.I am also getting segmentation fault error. Why is that? I am not accessing some uninitialized memory anywhere in program. Here is the code:

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

void* CreateChain(int*);

 int main()
{
int num;

pthread_t tid;


scanf("Enter the number of threads to create\n %d",&num);

pthread_create(&tid,NULL,CreateChain,&num);

pthread_join(tid,NULL);

printf("Thread No. %d is terminated\n",num);

return 0;
}

void* CreateChain(int* num )
 {
pthread_t tid;

if(num>0)
{
    pthread(&tid,NULL,CreateChain,num);
    pthread_join(tid,NULL);

    printf("Thread No. %d is terminated\n",*num);
}
else
    return NULL; 

return NULL;
}

I am getting following warnings and the Scanf prompt is not appearing for some reason.

enter image description here

Regards

Community
  • 1
  • 1
Alfred
  • 1,543
  • 7
  • 33
  • 45

3 Answers3

2

The pthread.h header file provides a forward declaration of pthread functions. This tells the compiler than these functions exist and have a certain signature. It doesn't however tell the linker anything about where to find these functions at runtime.

To allow the linker to resolve these calls (decide where to jump to inside your code or in a different shared object), you need to link against the appropriate (pthread) library by adding

-pthread

to your build command line.

[Note that it is also possible to use -lpthread. This previous question expains why -pthread is preferable.]

There are various other issues with the code that will be worthy of attention

  • The scanf line should be split into printf("Enter number of threads\n");scanf("%d", &num); to get the user prompt displayed
  • The signature of CreateChain is wrong - it should take a void* argument instead. You can always do something like int num = *(int*)arg; inside the function to retrieve the number of threads.
  • The logic inside CreateChain looks wrong. You currently compare a pointer against 0 - I presume you mean to compare the number of threads instead? Also, if you don't decrement the number of threads to create somewhere, you'll end up with code that creates threads forever (or until you run out of handles depending on how the different threads get scheduled).
Community
  • 1
  • 1
simonc
  • 41,632
  • 12
  • 85
  • 103
  • Although the problem is gone, but still the program is not compiling properly. I am not getting the prompt for getting no of threads to create from user. – Alfred Dec 04 '12 at 16:08
  • @Alfred I've updated my answer with a few issues you might like to consider – simonc Dec 04 '12 at 16:38
1

Try compiling like this below :

gcc -Wall -pthread test.c -o test.out

-pthread is an option to tell linker explicitly to resolve the symbols related to <pthread.h>

Omkant
  • 9,018
  • 8
  • 39
  • 59
0

add -lpthread

gcc -o test test.c -lpthread
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 2
    I think `-pthread` is preferred over your suggestion of `-lpthread`. [This previous question](http://stackoverflow.com/questions/2127797/gcc-significance-of-pthread-flag-when-compiling) explains why. – simonc Dec 04 '12 at 15:54