我如何强制cmake包含“-pthread”编译期间的选项? [英] How do I force cmake to include "-pthread" option during compilation?

查看:1462
本文介绍了我如何强制cmake包含“-pthread”编译期间的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一些像 find_package(Threads)的东西,但它似乎没有什么区别(至少本身)。现在我正在使用 SET(CMAKE_C_FLAGS $ {CMAKE_C_FLAGS}-pthread),但它看起来不是正确的解决方案。

解决方案

find_package(Threads)首先调用一个CMake模块,适合此平台的线程包,然后设置CMAKE_THREAD_LIBS_INIT变量(以及其他一些变量)。它不告诉CMake将任何可执行文件与它找到的任何线程库链接起来。您告诉CMake使用 target_link_libraries()命令将可执行文件与线程库链接起来。所以,举个例子,让我们说你的程序叫做测试。要将它链接到需要的线程:

$ p $ find_package(线程)
add_executable(test test.cpp)
target_link_libraries(test $ {CMAKE_THREAD_LIBS_INIT})


I know there is something like find_package(Threads) but it doesn't seem to make a difference (at least by itself). For now I'm using SET(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread"), but it doesn't look like a correct solution to me.

解决方案

find_package( Threads ) calls a CMake module that first, searches the file system for the appropriate threads package for this platform, and then sets the CMAKE_THREAD_LIBS_INIT variable (and some other variables as well). It does not tell CMake to link any executables against whatever threads library it finds. You tell CMake to link you executable against the "Threads" library with the target_link_libraries() command. So, for example lets say your program is called test. To link it against threads you need to:

find_package( Threads )
add_executable( test test.cpp )
target_link_libraries( test ${CMAKE_THREAD_LIBS_INIT} )

这篇关于我如何强制cmake包含“-pthread”编译期间的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆