c – Undefined reference to pthread_create in Linux

The Question :

408 people think this question is useful

I picked up the following demo off the web from https://computing.llnl.gov/tutorials/pthreads/

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&amp;threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

But when I compile it on my machine (running Ubuntu Linux 9.04) I get the following error:

corey@ubuntu:~/demo$ gcc -o term term.c
term.c: In function ‘main’:
term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
/tmp/cc8BMzwx.o: In function `main':
term.c:(.text+0x82): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

This doesn’t make any sense to me, because the header includes pthread.h, which should have the pthread_create function. Any ideas what’s going wrong?

The Question Comments :
  • Additionally: depending on the platform, you may need (a) a different compiler for threads, (b) a different libc for threads (i.e. -lc_r), (c) -thread or -threads or other, instead of or in addition to -lpthread.
  • Just a little above that example, you’ll see a table of the correct compiler commands, whether it be GCC, IBM, etc. ‘Employed Russian’ is correct.
  • Can you please unmark my answer, so that I can delete it (and mark the one that is actually correct, which is the highest-voted one)?
  • -lpthread is needed during compile
  • solution LDFLAGS= -pthread -lpthread

The Answer 1

739 people think this answer is useful

Both answers to this question so far are incorrect.
For Linux the correct command is:

gcc -pthread -o term term.c

In general, libraries should follow sources and objects on command line, and -lpthread is not an “option”, it’s a library specification. On a system with only libpthread.a installed,

gcc -lpthread ...

will fail to link.

The Answer 2

41 people think this answer is useful

in eclipse

properties->c/c++Build->setting->GCC C++ linker->libraries in top part add “pthread”

The Answer 3

29 people think this answer is useful

For Linux the correct command is:

gcc -o term term.c -lpthread

  1. you have to put -lpthread just after the compile command,this command will tell to the compiler to execute program with pthread.h library.
  2. gcc -l links with a library file.Link -l with library name without the lib prefix.

The Answer 4

26 people think this answer is useful

Running from the Linux terminal, what worked for me was compiling using the following command (suppose the c file I want to compile is called test.c):

gcc -o test test.c -pthread

Hope it helps somebody!

The Answer 5

16 people think this answer is useful

Acutally, it gives several examples of compile commands used for pthreads codes are listed in the table below, if you continue reading the following tutorial:

https://computing.llnl.gov/tutorials/pthreads/#Compiling

enter image description here

The Answer 6

14 people think this answer is useful

If you are using cmake, you can use:

add_compile_options(-pthread)

Or

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

The Answer 7

13 people think this answer is useful

I believe the proper way of adding pthread in CMake is with the following

find_package (Threads REQUIRED)

target_link_libraries(helloworld
    ${CMAKE_THREAD_LIBS_INIT}
)

The Answer 8

8 people think this answer is useful

Compile it like this : gcc demo.c -o demo -pthread

The Answer 9

5 people think this answer is useful

In Visual Studio 2019 specify -pthread in the property pages for the project under:

Linker -> Command Line -> Additional Options

Type in -pthread in the textbox.

The Answer 10

3 people think this answer is useful

You need to use the option -lpthread with gcc.

The Answer 11

3 people think this answer is useful

you need only Add “pthread” in proprieties=>C/C++ build=>GCC C++ Linker=>Libraries=> top part “Libraries(-l)”. thats it

The Answer 12

2 people think this answer is useful

check man page and you will get.

Compile and link with -pthread.

SYNOPSIS
       #include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);


       Compile and link with -pthread.
       ....

The Answer 13

0 people think this answer is useful

In Anjuta, go to the Build menu, then Configure Project. In the Configure Options box, add:

LDFLAGS='-lpthread'

Hope it’ll help somebody too…

The Answer 14

0 people think this answer is useful

Sometimes, if you use multiple library, check the library dependency. (e.g. -lpthread -lSDL… <==> … -lSDL -lpthread)

Add a Comment