How to Compile C Programs with POSIX Threads on Linux

Installing Pthread On Linux

On Linux, pthread support comes from the system C library. You normally do not install a package named pthread. Include pthread.h, compile with GCC’s -pthread option, and use the POSIX thread application programming interface in your program.

Terminal illustration for installing pthread on Linux
POSIX thread programs use the system toolchain and the pthread API.

Compile a pthread program with GCC

Save this program as pthread_demo.c.

#include <pthread.h>
#include <stdio.h>

static void *worker(void *arg) {
    const char *message = arg;
    printf("%s\n", message);
    return NULL;
}

int main(void) {
    pthread_t thread;
    if (pthread_create(&thread, NULL, worker, "worker thread ran") != 0) return 1;
    if (pthread_join(thread, NULL) != 0) return 1;
    puts("main thread finished");
    return 0;
}

Compile and run it with this command. The recorded GCC run printed worker thread ran followed by main thread finished.

gcc -Wall -Wextra -pthread pthread_demo.c -o pthread_demo && ./pthread_demo

The -pthread option affects compilation and linking. GCC documents it as the thread-support option, so use it for a POSIX thread program instead of adding -lpthread only at the end of the command.

Install a C development toolchain

You need a C compiler and the development files supplied by your Linux distribution. On Debian or Ubuntu, install build-essential. Fedora, RHEL, and compatible systems use Development Tools, while Arch Linux supplies GCC through the gcc package.

Distribution familyCommand
Debian or Ubuntusudo apt install build-essential
Fedora or RHELsudo dnf group install “Development Tools”
Arch Linuxsudo pacman -S gcc

Check the installed thread implementation with getconf GNU_LIBPTHREAD_VERSION. The test command returned NPTL 2.41, the GNU Native POSIX Thread Library interface used by the compiler.

Why a pthread package is missing

pthread is an interface defined by POSIX, not a separate runtime package that ordinary Linux C programs fetch from a repository. The header and implementation arrive with the C library and development toolchain.

If GCC reports pthread.h: No such file or directory, install your distribution’s compiler development package. If linking reports an undefined reference to pthread_create, add -pthread to the GCC command and compile every source file that uses thread-aware headers with the same option.

Use an explicit thread lifecycle

pthread_create receives a function that returns void pointer and one argument pointer. pthread_join gives the calling thread a defined point to wait for completion.

Pass shared data only after deciding who owns it and when it can change. Create and join are enough for this example, but shared counters and collections need a mutex or another synchronization primitive before concurrent writes begin.

Continue with Linux C tooling

These LinuxForDevices tutorials help with the next compiler or build task.

Do I need to install pthread separately on Linux?

No. POSIX thread support is supplied through the Linux C library and development toolchain. Install a compiler development package when pthread.h is unavailable, then compile with GCC’s -pthread option.

Should I use -pthread or -lpthread with GCC?

Use -pthread for a POSIX thread program. GCC applies thread-related compiler settings and links the thread support library.

Why does pthread_create produce an undefined reference error?

Compile and link the program with -pthread. The error means the linker did not receive the required thread-support option.

Keep -pthread in your build command, then add synchronization only where shared state requires it. That gives you a small program whose thread lifetime is explicit before the program grows.