Encountering a "terminate called" error when attempting to run std::thread code in G ? Let's delve into the issue.
The code provided utilizes std::thread, which is typically implemented using pthreads on Linux. To resolve the linking issue, the -pthread compiler option is necessary. This option informs the compiler to link against the pthreads library.
Crucially, the -pthread option must be placed after the source files to ensure proper linking:
g++ -std=c++0x test.cpp -pthread
This will instruct the compiler to link the program with the pthreads library, enabling the use of std::thread. Without this option, the linker will fail to find the necessary symbols from the pthreads library, resulting in the observed error.
The above is the detailed content of Why Does My g std::thread Code Fail to Link on Linux, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!