ld "undefined reference to pthread_create" (missing -pthread) in CI
pthread_create and friends live in the threads runtime. Without -pthread on both compile and link, the symbols are undefined at link time.
What this error means
Code that uses std::thread or raw pthreads fails to link with "undefined reference to pthread_create", though it compiles cleanly.
ld
/usr/bin/ld: main.o: in function 'main':
main.cpp:(.text+0x2c): undefined reference to 'pthread_create'
collect2: error: ld returned 1 exit statusCommon causes
How to fix it
Add -pthread
- Pass -pthread to both compilation and linking.
- In CMake, link Threads::Threads via find_package(Threads REQUIRED).
ld
g++ -pthread main.cpp -o app
# CMake
find_package(Threads REQUIRED)
target_link_libraries(app PRIVATE Threads::Threads)How to prevent it
- Use -pthread (or CMake Threads::Threads) for any code that touches std::thread or pthreads so the runtime is always linked.
Frequently asked questions
What causes ""undefined reference to pthread_create""?
Code that uses std::thread or raw pthreads fails to link with "undefined reference to pthread_create", though it compiles cleanly.
How do I fix "undefined reference to pthread_create"?
Add -pthread
Related guides
ld "DSO missing from command line" in CIFix ld "DSO missing from command line" in CI - a symbol resolves through a shared library that was relied on…
CMake "Could NOT find Threads" in CIFix CMake "Could NOT find Threads" in CI - the threads probe failed because no working compiler or pthread su…
ld "undefined reference to `main'" in CIFix ld "undefined reference to `main'" in CI - the link produced an executable with no main, usually because…