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

后端 未结 3 931
别跟我提以往
别跟我提以往 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:40

    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} )
    

提交回复
热议问题