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

2
  • 2
    Add -pthread to the build command. Commented Dec 4, 2012 at 15:43
  • I don't believe that you're getting link errors when "executing" the code. Possibly when compiling; please clarify. Commented Dec 4, 2012 at 15:49

3 Answers 3

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).
Sign up to request clarification or add additional context in comments.

2 Comments

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 I've updated my answer with a few issues you might like to consider
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>

1 Comment

+1 for finding a way to sneak in a recommendation for -Wall
0

add -lpthread

gcc -o test test.c -lpthread

1 Comment

I think -pthread is preferred over your suggestion of -lpthread. This previous question explains why.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.