There are already answered questions with how to do threads with cmake, but none has an example, and what I tried is not working.
So, I took cmake project with directories and tests, and modified CMakeLists.txt for the test.
I changed this:
# 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
into this:
# 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
Note the changed lines, as described in other answers (for example, this):
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
and
target_link_libraries(testlib PRIVATE modern_library Catch2::Catch2 Threads::Threads)
Then I create makefiles with
mkdir build
cmake -B. -S..
when I compile, with make VERBOSE
, I see that -pthread
flags are not added to compilation and linking.
So, how to modify the CMakeList.txt for test to get -pthread
compilation and linking flags?
EDIT: I tried the version 11.3.0 of g++. One is g++ cross compiler:
build$ $CXX --version
aarch64-ifmlinux-linux-g++ (GCC) 11.3.0
another is installed on my ubuntu:
g++ --version
g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
The output of make VERBOSE=1
is:
[ 88%] Building CXX object tests/CMakeFiles/testlib.dir/testlib.cpp.o
cd /home/dejovivl/test/modern-cmake/examples/extended-project/build/tests && /usr/bin/c++ -I/home/dejovivl/test/modern-cmake/examples/extended-project/src/../include -I/home/dejovivl/test/modern-cmake/examples/extended-project/build/_deps/catch-src/single_include -std=c++17 -MD -MT tests/CMakeFiles/testlib.dir/testlib.cpp.o -MF CMakeFiles/testlib.dir/testlib.cpp.o.d -o CMakeFiles/testlib.dir/testlib.cpp.o -c /home/dejovivl/test/modern-cmake/examples/extended-project/tests/testlib.cpp
[100%] Linking CXX executable testlib
cd /home/dejovivl/test/modern-cmake/examples/extended-project/build/tests && /usr/bin/cmake -E cmake_link_script CMakeFiles/testlib.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/testlib.dir/testlib.cpp.o -o testlib ../src/libmodern_library.a
make[2]: Leaving directory '/home/dejovivl/test/modern-cmake/examples/extended-project/build'
[100%] Built target testlib
make[1]: Leaving directory '/home/dejovivl/test/modern-cmake/examples/extended-project/build'
/usr/bin/cmake -E cmake_progress_start /home/dejovivl/test/modern-cmake/examples/extended-project/build/CMakeFiles 0