How do I force cmake to include “-pthread” option during compilation?

后端 未结 3 936
别跟我提以往
别跟我提以往 2020-11-29 22:43

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

3条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 23:32

    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

提交回复
热议问题