https://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linuxhttps://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linuxI know this might be a little late, but. If you cannot find the C/C++ Build setting in the properties (I couldn't, maybe it's by installation or a bug), then there is a direct lower level workaround using the CMakeLists.txt file. You need to insert
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
before the add_executable command. This will instruct the linker to do the same (see CMAKE_EXE_LINKER_FLAGS and SET documentation for more help).
我知道可能有点晚了,但是。如果在属性中找不到C/ c++ Build设置(我找不到,可能是安装或bug的原因),那么可以使用CMakeLists.txt文件来直接解决较低级别的问题。你需要在add_executable命令之前插入SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")。这将指示链接器做同样的事情(参见CMAKE_EXE_LINKER_FLAGS和SET文档获得更多帮助)。
-------------------------------------------------------------------------------------------------------------------------
If you are using cmake, you can use:
如果你正在使用cmake,你可以使用:
add_compile_options(-pthread)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
-------------------------------------------------------------------------------------------------------------------------
I believe the proper way of adding pthread
in CMake
is with the following
我相信在CMake中添加pthread的正确方法是如下所示
find_package (Threads REQUIRED)
target_link_libraries(helloworld
${CMAKE_THREAD_LIBS_INIT}
)
最终解决方案:
set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -pthread”)
# 增加pthread 链接选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")