如何强制 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CMake 最新版本 (>= 3.1) 中的 Threads 模块生成 Threads::Threads 导入目标。将目标链接到 Threads::Threads 会添加所有必要的编译和链接标志。可以这样完成:
根据 CMake 文档
The Threads module in the latest versions (>= 3.1) of CMake generates the Threads::Threads imported target. Linking your target against Threads::Threads adds all the necessary compilation and linking flags. It can be done like this:
Use of the imported target is highly recommended for new code, according to the CMake docs
find_package( Threads )
调用 CMake 模块,该模块首先在文件系统中搜索适合该平台的线程包,然后设置 CMAKE_THREAD_LIBS_INIT 变量(以及其他一些变量)。它不会告诉 CMake 将任何可执行文件链接到它找到的任何线程库。您可以使用target_link_libraries()
命令告诉 CMake 将可执行文件链接到“Threads”库。例如,假设您的程序名为 test。要将其链接到线程,您需要: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 thetarget_link_libraries()
command. So, for example lets say your program is called test. To link it against threads you need to:怎么样:
How about the following:
如果我明确指定默认入口点和要使用的库,则编译不会出现问题。这里的默认入口点是在cmake中指定版本。
cmake_minimum_required(...)、target_link_libraries(...)
下面是一个例子。
If I explicitly specify the default entry point and the library to use, it compiles without problems. The default entry point here is to specify the version in cmake.
cmake_minimum_required(...), target_link_libraries(...)
Below is an example.