Linux pthread compilation details, linuxpthread details
When I write a multi-threaded program in linux today, the header file <pthread. h> is included in the. c file, but the following error is reported during compilation: Undefined reference to 'pthread _ create'
After checking the Internet, the original pthread library is not the default library of linux, so you need to manually link it during compilation. The procedure is as follows:
Gcc mian. c-lpthread
Since I am a code written under clion, I want clionto Support automatic link to the pthreaddatabase, and add the following statement to the cmakelists.txt file of the project objective:
Find_package (Threads REQUIRED)
Target_link_libraries (Execution file name Threads: Threads)
The first parameter in target_link_libraries indicates the name of the execution file.
Add_executable (Execution file name $ {SOURCE_FILES })
Make sure that the two execution files are the same. This name can be defined as needed.