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

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

问题描述

我知道有类似于 find_package(Threads) 的东西,但它似乎没有什么区别(至少就其本身而言).现在我正在使用 SET(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread"),但它对我来说似乎不是一个正确的解决方案.

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 ) 调用 CMake 模块,该模块首先在文件系统中搜索适合该平台的线程包,然后设置 CMAKE_THREAD_LIBS_INIT 变量(以及其他一些变量).它不会告诉 CMake 将任何可执行文件链接到它找到的任何线程库.您告诉 CMake 使用 target_link_libraries() 命令将您的可执行文件链接到线程"库.因此,例如,假设您的程序称为测试.要将其链接到您需要的线程:

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天全站免登陆