undefined reference to pthread_create C/C++ Origin: Application & API OSI layer 7 · Application 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):

cannot find -l — /usr/bin/ld: cannot find -lssl: No such file or directory (C/C++)cannot find -ldata race — ThreadSanitizer: data race (C/C++)data raceE0373: closure may outlive the current function — Rust: error[E0373]: closure may outlive the current function, but it borrows `v`, which is owned by the current function (Rust)E0373: closure may ou…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…error: linker `cc` not found — Rust: error: linker `cc` not found — note: No such file or directory (os error 2) (Rust)error: linker `cc` no…file in wrong format — ld: file in wrong format / skipping incompatible libfoo.so when searching (C/C++)file in wrong formatLNK1104 — 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 ofundefined…

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.