2

When working with POSIX threads in a C program, I found "undefined reference to pthread create" error.

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

void *myThreadFun(void *vargp)
{ 
  sleep(1); 
  printf("Printing Sample \n"); 
  return NULL;
}

Although my gcc and g++ compilers are working pretty fine. Moreover, I am compiling this code using this command:

gcc test.c -o test
1
  • @steeldriver, edited and provided all the necessary details. Dec 6, 2022 at 13:41

1 Answer 1

2

The error “undefined reference to pthread create” is caused when the pthread header is not included in the program file. Ensure that the header file is included properly as follows.

#include <pthread.h>

The other cause is that pthread is not used in the command line while compiling the code. To include the thread while compilation, use the syntax.

gcc -pthread -o threads <cprogram>.c

Found this solution on this post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.