Score:3

undefined reference to pthread create error

pn flag

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
Chemi Saffron avatar
pn flag
@steeldriver, edited and provided all the necessary details.
Score:3
sc flag

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

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.