Solutions For Error “Undefined reference to ‘pthread_create'” in Linux

Undefined reference to ‘pthread_create’ in Linux

Many developers run into the error “Undefined reference to ‘pthread_create’” in Linux when they try out the multithreading support of C for the first time. It happens due to some basic mistakes you can easily avoid. Read on to find out.

Reproduce The Error “Undefined reference to ‘pthread_create'” in Linux

This is a simple example of making use of the POSIX Threads libraries in C to create and execute several threads:

void *print_return( void *str_ptr ) {
    char *str;
    str = (char *) str_ptr;
    printf("%s \n", str);
}

int main() {    
    char *str1 = "This is the first thread.";
    char *str2 = "This is the second thread.";
    pthread_t thread1, thread2;
    int  return1, return2;
    return1 = pthread_create( &thread1, NULL, print_return, (void*) str1);
    return2 = pthread_create( &thread2, NULL, print_return, (void*) str2);
    pthread_join( thread1, NULL);
    pthread_join( thread2, NULL);
    
    printf("Thread 1 ends and returns %d\n", return1);
    printf("Thread 2 ends and returns %d\n", return2);
    exit(0);
}

This program uses the pthread_create() function to create two threads, which print their own message to the output console. We wait for their termination with the pthread_join() function. This pauses the main() function until those two threads end.

On success, they return 0. If there are errors, an error number is returned. Finally, we print these numbers with printf() in the main() function.

It should produce an output like this:

This is the first thread. 
This is the second thread. 
Thread 1 ends and returns 0
Thread 2 ends and returns 0

However, you may run into this error instead while compiling the source file with gcc:

undefined reference to `pthread_create'

This is one of the most common errors during the linking stage of the compilation process. In particular, it indicates that the compiler can’t find the reference for the pthread_create() function in our example. Many things can lead to this problem.

Causes And Solutions

Missing Header File

In Linux, the POSIX thread interface is provided by the pthread.h header file. It provides function mappings and declarations for threading interfaces, including constants used by these functions.

The location of the header file in your file system depends on your platform and C library implementation. You must explicitly add this line to the beginning of the source file to include it to your program so the compiler can find the references to functions like pthread_create() and pthread_join():

#include <pthread.h>

Missing GCC Option

If the source file has included the header file and you compile with GNU Compiler Collection (GCC – the most popular C compiler in Linux), don’t forget to add the -pthread option:

gcc -pthread example.c -o example

This option enables support for multithreading, using the pthreads library in your system. It sets flags for both the linker and preprocessor.

Missing Makefile Flag

Makefiles are a common method for organizing your project. If you are on Linux, the big chance is that you are using GNU Make. You can put the rules in a makefile to remove the need for typing the compiling commands every time you need to build your program.

Since this file instructs the gcc command on how to compile your source file, you can add the -pthread option directly to the compilation rule:

all:

gcc -pthread example.c -o example

Makefiles also support flags to make them more readable:

CC = gcc

LDFLAGS = -pthread

all:

${CC} ${LDFLAGS} example.c -o example

Summary

The error Undefined reference to ‘pthread_create’ in Linux happens when you use the pthreads interface but don’t declare it properly. Make sure you have included the pthread.h file and set the right flag for your compiler.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *