0

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
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • According to [documentation](https://cmake.org/cmake/help/latest/module/FindThreads.html), variable `THREADS_PREFER_PTHREAD_FLAG` stands the **preference**: "This variable has no effect if the system libraries provide the thread functions". Since you don't specify which compiler do you use, we can only guess whether this sentence is applicable for your case. "I see that `-pthread` flags are not added to compilation and linking." - Please, add to the question post that compilation and linking command lines. – Tsyvarev Feb 27 '23 at 12:12
  • @Tsyvarev Modified the question. The g++ version is 11.3.0, and as you can see in the output, there is no `-pthread` added to the compilation and linking flags. – BЈовић Feb 27 '23 at 14:24
  • 2
    Newer version of glibc seems to have pthread library incorporated into the main (libc) library: https://developers.redhat.com/articles/2021/12/17/why-glibc-234-removed-libpthread. And CMake is able detect that. You may inspect output of **configuration** process and find lines about searching threads. If you have `Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success`, then pthread functions are available without pthread and other libraries: https://stackoverflow.com/a/64515538/3440745 – Tsyvarev Feb 27 '23 at 14:50
  • @Tsyvarev oh didn't know that, and didn't find that answer. Thank you – BЈовић Feb 27 '23 at 14:59
  • "`mkdir build`" "`cmake -B. -S.."" are you missing a `cd build` in between those? – starball Feb 27 '23 at 17:37
  • @Tsyvarev, libpthread being merged into glibc is largely a separate matter from whether GCC's `-pthread` option is or should be used. `-pthread` is both a preprocessor option and a link option, and it should be used consistently for those purposes when building a program that (directly) accesses pthreads interfaces. This is not the same as `-lpthread`, which was never appropriate, and now is moot. – John Bollinger Feb 28 '23 at 20:54
  • @JohnBollinger: Yes, I remember that `-pthread` is something larger than `-lpthread`. But CMake truly omits `-pthread` if it finds that threads functions are implemented in the glibc: https://github.com/Kitware/CMake/blob/master/Modules/FindThreads.cmake. (`_threads_check_libc()` is called first, and on success it sets `Threads_FOUND` variable. Function `_threads_check_flag_pthread()` is called afterwards, and if it finds the variable `Threads_FOUND` to be already set then it does nothing.) I am not sure whether it is a bug in CMake or it is acceptable to use threads without `-pthread`. – Tsyvarev Feb 28 '23 at 21:05
  • @Tsyvarev yes, I thought the same - that is is a bug in cmake. But if pthread is integrated in system libraries, then maybe it is not needed. Cant find anything about it :( – BЈовић Mar 01 '23 at 09:57

0 Answers0