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.
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' 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::threador pthread functions used without-pthreaddlopen/dlsymwithout-ldlon older glibcclock_gettime/shm_openwithout-lrton older glibc- Library listed before the object files that need it
⚡ Quick fix
- Add
-pthreadto compile and link flags - Link
Threads::Threadsin CMake - Put libraries after the sources:
g++ main.cpp -pthread -ldl
Detailed fix by platform
GCC / Clang
g++ -std=c++17 main.cpp -o app -pthread- Order matters for static libraries: sources first, then
-lflags
CMake
find_package(Threads REQUIRED)andtarget_link_libraries(app PRIVATE Threads::Threads)
Code examples
Linking threads correctly
find_package(Threads REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE Threads::Threads ${CMAKE_DL_LIBS})How to diagnose
- Symbol — Which function is missing?
- Flag — Is -pthread used at link time?
- 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:
Similar errors
- error while loading shared libraries error while loading shared libraries: libX.so.N: cannot open shared object file: No such file or directory
- LNK1104 MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running)
- multiple definition of C/C++: multiple definition of `x'; first defined here — ld returned 1 exit status
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 15 Oct 2026