undefined reference to pthread_create C/C++ Origin: Application & API Last updated 15 Oct 2026

C/C++: undefined reference to `pthread_create' — missing -pthread (also `dlopen`, `clock_gettime`)

Your program uses threads (or another system library) but the linker was not told to link it.

Exact message you may see undefined reference to `pthread_create'undefined reference to `dlopen'undefined reference to `clock_gettime'undefined reference to `std::thread::_M_start_threadundefined reference to `sqrt'
Seen on: C/C++ GCC Clang CMake

Meaning

On many Linux systems the thread functions live in libpthread, and dlopen in libdl, clock_gettime in librt. With glibc 2.34+ these moved into libc, but older systems and static builds still need the flag. Use -pthread (compile and link) or CMake's Threads::Threads.

Common causes

  • std::thread or pthread functions used without -pthread
  • dlopen/dlsym without -ldl on older glibc
  • clock_gettime/shm_open without -lrt on older glibc
  • Library listed before the object files that need it

⚡ Quick fix

  1. Add -pthread to compile and link flags
  2. Link Threads::Threads in CMake
  3. Put libraries after the sources: g++ main.cpp -pthread -ldl

Detailed fix by platform

GCC / Clang

  1. g++ -std=c++17 main.cpp -o app -pthread
  2. Order matters for static libraries: sources first, then -l flags

CMake

  1. find_package(Threads REQUIRED) and target_link_libraries(app PRIVATE Threads::Threads)

Code examples

Linking threads correctly

cmake
find_package(Threads REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE Threads::Threads ${CMAKE_DL_LIBS})

How to diagnose

  1. Symbol — Which function is missing?
  2. Flag — Is -pthread used at link time?
  3. Order — Are libraries after the objects?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

error while loading shared libraries — error while loading shared libraries: libX.so.N: cannot open shared object file: No such file or directory (Linux)error while loading s…LNK1104 — MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running) (C/C++)LNK1104multiple definition of — C/C++: multiple definition of `x'; first defined here — ld returned 1 exit status (C/C++)multiple definition ofterminate called without an active exception — C++: terminate called without an active exception — std::thread destroyed while joinable (C/C++)terminate called with…undefined reference to — C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status (C/C++)undefined reference toundefined reference to main — C/C++: undefined reference to `main' — (.text+0x20) in crt1.o (C/C++)undefined reference t…undefined reference to vtable — C++: undefined reference to `vtable for X' — virtual function not defined (C/C++)undefined reference t…undefined…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.