undefined reference to `pthread_create' c++
Explanation of the "undefined reference to `pthread_create'" Error in C++
Understanding the Error Message When you encounter the error "undefined reference to
pthread_create'", it indicates that the linker cannot find the implementation for the
pthread_create` function, which is part of the POSIX threads (pthreads) library for multi-threading in C++.Including the pthread Library To resolve this error, you need to link your C++ program with the pthread library. This can be achieved by adding the
-pthread
option to the compiler command when compiling and linking your program.Compiler Command with pthread Library For example, if you are using the g++ compiler, the command to compile and link your program with the pthread library would be:
g++ your_program.cpp -o your_program -pthread
Explanation of
-pthread
Option The-pthread
option instructs the compiler to link the pthread library, which provides the necessary implementation for functions likepthread_create
, allowing your program to use POSIX threads for multi-threading.Recompiling and Linking After adding the
-pthread
option to your compiler command, recompile and link your C++ program to resolve the "undefined reference to `pthread_create'" error.