如何强制 cmake 包含“-pthread”编译时的选项?

发布于 2024-10-25 12:36:07 字数 145 浏览 8 评论 0原文

我知道有类似 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏天碎花小短裙 2024-11-01 12:36:07

CMake 最新版本 (>= 3.1) 中的 Threads 模块生成 Threads::Threads 导入目标。将目标链接到 Threads::Threads 会添加所有必要的编译和链接标志。可以这样完成:

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

add_executable(test test.cpp)
target_link_libraries(test 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:

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

add_executable(test test.cpp)
target_link_libraries(test Threads::Threads)

Use of the imported target is highly recommended for new code, according to the CMake docs

眉黛浅 2024-11-01 12:36:07

find_package( Threads ) 调用 CMake 模块,该模块首先在文件系统中搜索适合该平台的线程包,然后设置 CMAKE_THREAD_LIBS_INIT 变量(以及其他一些变量)。它不会告诉 CMake 将任何可执行文件链接到它找到的任何线程库。您可以使用 target_link_libraries() 命令告诉 CMake 将可执行文件链接到“Threads”库。例如,假设您的程序名为 test。要将其链接到线程,您需要:

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

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} )
不醒的梦 2024-11-01 12:36:07

怎么样:

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
if(CMAKE_USE_PTHREADS_INIT)
    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread")
elseif(...)
    ...
endif()
add_executable( test test.cpp )
target_link_libraries( test ${CMAKE_THREAD_LIBS_INIT} )

How about the following:

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
if(CMAKE_USE_PTHREADS_INIT)
    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread")
elseif(...)
    ...
endif()
add_executable( test test.cpp )
target_link_libraries( test ${CMAKE_THREAD_LIBS_INIT} )
长不大的小祸害 2024-11-01 12:36:07

如果我明确指定默认入口点和要使用的库,则编译不会出现问题。这里的默认入口点是在cmake中指定版本。
cmake_minimum_required(...)、target_link_libraries(...)
下面是一个例子。

# important
cmake_minimum_required(VERSION 2.8)

project(main)

# set c++ version & etc...
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# important
find_package( Threads )

add_executable(main main.cpp)

# important
target_link_libraries(main ${CMAKE_THREAD_LIBS_INIT})

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.

# important
cmake_minimum_required(VERSION 2.8)

project(main)

# set c++ version & etc...
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# important
find_package( Threads )

add_executable(main main.cpp)

# important
target_link_libraries(main ${CMAKE_THREAD_LIBS_INIT})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文