公告
财富商城
积分规则
提问
发文
2019-01-02 22:24发布
对你真心纯属浪费
I'm running RHEL 5.1 and use gcc.
RHEL 5.1
gcc
How I tell cmake to add -pthread to compilation and linking?
cmake
-pthread
@Manuel was part way there. You can add the compiler option as well, like this:
If you have CMake 3.1.0+, this becomes even easier:
set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) target_link_libraries(my_app Threads::Threads)
If you are using CMake 2.8.12+, you can simplify this to:
find_package(Threads REQUIRED) if(THREADS_HAVE_PTHREAD_ARG) target_compile_options(my_app PUBLIC "-pthread") endif() if(CMAKE_THREAD_LIBS_INIT) target_link_libraries(my_app "${CMAKE_THREAD_LIBS_INIT}") endif()
Older CMake versions may require:
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()
If you want to use one of the first two methods with CMake 3.1+, you will need set(THREADS_PREFER_PTHREAD_FLAG ON) there too.
set(THREADS_PREFER_PTHREAD_FLAG ON)
Here is the right anwser:
ADD_EXECUTABLE(your_executable ${source_files}) TARGET_LINK_LIBRARIES( your_executable pthread )
equivalent to
-lpthread
The following should be clean (using find_package) and work (the find module is called FindThreads):
find_package
FindThreads
cmake_minimum_required (VERSION 2.6) find_package (Threads) add_executable (myapp main.cpp ...) target_link_libraries (myapp ${CMAKE_THREAD_LIBS_INIT})
最多设置5个标签!
@Manuel was part way there. You can add the compiler option as well, like this:
If you have CMake 3.1.0+, this becomes even easier:
If you are using CMake 2.8.12+, you can simplify this to:
Older CMake versions may require:
If you want to use one of the first two methods with CMake 3.1+, you will need
set(THREADS_PREFER_PTHREAD_FLAG ON)
there too.Here is the right anwser:
equivalent to
The following should be clean (using
find_package
) and work (the find module is calledFindThreads
):