Undefined reference to pthread_create in Linux

https://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linuxicon-default.png?t=M85Bhttps://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")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值