cmake et libpthread

RHEL 5.1 et utilise gcc .

Comment je dis à cmake d’append -pthread à la compilation et aux liens?

Les éléments suivants doivent être propres (en utilisant find_package ) et fonctionnent (le module find s’appelle FindThreads ):

 cmake_minimum_required (VERSION 2.6) find_package (Threads) add_executable (myapp main.cpp ...) target_link_libraries (myapp ${CMAKE_THREAD_LIBS_INIT}) 

@Manuel était en partie là. Vous pouvez également append l’option de compilation, comme ceci:

Si vous avez CMake 3.1.0+, cela devient encore plus facile :

 set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) target_link_libraries(my_app Threads::Threads) 

Si vous utilisez CMake 2.8.12+, vous pouvez simplifier cela pour:

 find_package(Threads REQUIRED) if(THREADS_HAVE_PTHREAD_ARG) target_comstack_options(my_app PUBLIC "-pthread") endif() if(CMAKE_THREAD_LIBS_INIT) target_link_libraries(my_app "${CMAKE_THREAD_LIBS_INIT}") endif() 

Les anciennes versions de CMake peuvent nécessiter:

 find_package(Threads REQUIRED) if(THREADS_HAVE_PTHREAD_ARG) set_property(TARGET my_app PROPERTY COMPILE_OPTIONS "-pthread") set_property(TARGET my_app PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread") endif() if(CMAKE_THREAD_LIBS_INIT) target_link_libraries(my_app "${CMAKE_THREAD_LIBS_INIT}") endif() 

Si vous souhaitez utiliser l’une des deux premières méthodes avec CMake 3.1+, vous devez également set(THREADS_PREFER_PTHREAD_FLAG ON) .

Voici la bonne réponse:

 ADD_EXECUTABLE(your_executable ${source_files}) TARGET_LINK_LIBRARIES( your_executable pthread ) 

équivalent à

 -lpthread