如何在使用 cmake 编译时正确获取 -pthread 标志?

问题描述 投票:0回答:0

已经有关于如何使用 cmake 做线程的问题的答案,但是没有一个例子,我试过的也不起作用。

所以,我将 cmake 项目与目录和测试,并修改了 CMakeLists.txt 用于测试.

我改变了这个:

# Testing library
FetchContent_Declare(
  catch
  GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  GIT_TAG v2.13.6)
FetchContent_MakeAvailable(catch)
# Adds Catch2::Catch2

# Tests need to be added as executables first
add_executable(testlib testlib.cpp)

# I'm using C++17 in the test
target_compile_features(testlib PRIVATE cxx_std_17)

# Should be linked to the main library, as well as the Catch2 testing library
target_link_libraries(testlib PRIVATE modern_library Catch2::Catch2)

# If you register a test, then ctest and make test will run it.
# You can also run examples and check the output, as well.
add_test(NAME testlibtest COMMAND testlib) # Command can be a target

进入这个:

# Testing library
FetchContent_Declare(
  catch
  GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  GIT_TAG v2.13.6)
FetchContent_MakeAvailable(catch)
# Adds Catch2::Catch2

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)


# Tests need to be added as executables first
add_executable(testlib testlib.cpp)

# I'm using C++17 in the test
target_compile_features(testlib PRIVATE cxx_std_17)

# Should be linked to the main library, as well as the Catch2 testing library
target_link_libraries(testlib PRIVATE modern_library Catch2::Catch2 Threads::Threads)

# If you register a test, then ctest and make test will run it.
# You can also run examples and check the output, as well.
add_test(NAME testlibtest COMMAND testlib) # Command can be a target

然后我用 mkdir 构建 cmake -B。 -S..

当我用

make VERBOSE
编译时,我看到
-pthread
标志没有添加到编译和链接中。

那么,如何修改用于测试的 CMakeList.txt 以获得

-pthread
编译和链接标志?

linux cmake pthreads
© www.soinside.com 2019 - 2024. All rights reserved.