Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:45:29+00:00 2026-06-15T08:45:29+00:00

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

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:45:30+00:00Added an answer on June 15, 2026 at 8:45 am

    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).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: sem_open() error: “undefined reference to sem_open()” on linux (Ubuntu 10.10) Having issues
Possible Duplicate: C++: undefined reference to static class member The following C++ code compiles
Possible Duplicate: Undefined Behavior and Sequence Points after the following c++ code, the array
Possible Duplicate: What does it mean to have an undefined reference to a static
Possible Duplicate: PHP: “Notice: Undefined variable” and “Notice: Undefined index” This code shows warnings
Possible Duplicate: Undefined Symbol ___gxx_personality_v0 on link I have a problem with the following
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: Undefined reference - C++ linker error Now, I'm getting an Undefined reference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.