261

I'm running RHEL 5.1 and use gcc.

How I tell cmake to add -pthread to compilation and linking?

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
dimba
  • 26,717
  • 34
  • 141
  • 196

4 Answers4

305

@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 PRIVATE 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.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
thehouse
  • 7,957
  • 7
  • 33
  • 32
  • It seems like e.g. CMake 3.0.2 won't set THREADS_HAVE_PTHREAD_ARG if pthread library was found (`NOT CMAKE_HAVE_THREADS_LIBRARY`), e.g. on Ubuntu 15.04 :( – mlt Jun 08 '15 at 21:58
  • 1
    The version given for CMake 2.8.12+ won't work as intended, because of how the code works in `/usr/share/cmake-2.8/Modules/FindThreads.cmake` (eg. see here https://www.apt-browse.org/browse/ubuntu/trusty/main/all/cmake-data/2.8.12.2-0ubuntu3/file/usr/share/cmake-2.8/Modules/FindThreads.cmake) Basically, `THREADS_HAVE_PTHREAD_ARG` is only set if the other variations of the flag weren't found (ie. `-lpthread`, `-lpthread`, or `-lthread`) – j1elo Jul 02 '17 at 14:16
  • This gives the error "Cannot specify link libraries for target "my_app" which is not built by this project." -- how do you enable it universally without having to do it individually for each target? – Chris Dodd Nov 22 '19 at 19:07
  • 1
    Is the first still the easiest way in 2020/ cmake 3.17? – Jay May 04 '20 at 10:56
  • 1
    What happens if you don't set the THREADS_PREFER_PTHREAD_FLAG flag, just find and link? – Jay May 04 '20 at 11:19
  • I had to use `target_link_libraries(my_app INTERFACE Threads::Threads)` – Matthias Kuhn Aug 10 '21 at 04:56
213

The following should be clean (using find_package) and work (the find module is called FindThreads):

cmake_minimum_required (VERSION 2.6) 
find_package (Threads)
add_executable (myapp main.cpp ...)
target_link_libraries (myapp ${CMAKE_THREAD_LIBS_INIT})
Manuel
  • 6,461
  • 7
  • 40
  • 54
  • 2
    This doesn't work when using in a CXX only project. Does anyone know a nice workaround? – Simon Nov 29 '11 at 03:46
  • 2
    @Simon a bit late but please see this: http://stackoverflow.com/questions/15193785/how-to-get-cmake-to-recognize-pthread-on-ubuntu – congusbongus Mar 04 '13 at 02:57
  • 1
    This solution works accros Mac OS X, Centos 6 and Solaris 10. – bernardn Oct 19 '14 at 18:49
  • 1
    Note that this won't [set -pthread compiler flag](http://stackoverflow.com/q/2127797/673826) – mlt Jun 08 '15 at 22:00
  • is `find_package` also the approch on windows for .dll dependencies? Because usually, you have your lib dependencies in a thirdParty directory... but cmake looks in a `IMPORTED_LOCATION` path, isn't it? – Sandburg Feb 04 '19 at 16:02
36

Here is the right anwser:

ADD_EXECUTABLE(your_executable ${source_files})

TARGET_LINK_LIBRARIES( your_executable
pthread
)

equivalent to

-lpthread
iAdjunct
  • 2,739
  • 1
  • 18
  • 27
Nadir SOUALEM
  • 3,451
  • 2
  • 23
  • 30
  • 1
    This is equivalent. "-pthread" donates much more - at compilation it's -D_REENTRANT, at link time -lpthread. On some system at even can be more than this. – dimba Nov 01 '09 at 08:15
  • 2
    SET(CMAKE_CXX_FLAGS_DEBUG "... -lpthread") SET(CMAKE_CXX_FLAGS_RELEASE "... -lpthread") – Nadir SOUALEM Nov 01 '09 at 10:42
  • I feel like this answer directly answers the question. I agree that using the Threads find module is more portable, but it is not what was asked. – JonnyRo Jul 10 '13 at 01:16
  • 24
    this solution bricks as soon as you change from linux to freebsd or windows. – Alexander Oh Aug 07 '13 at 09:07
  • 5
    Doesn't work, "target_link_libraries(your_executable pthread)" is how it should be written. – Overv Jan 22 '15 at 12:17
  • 10
    This is not the right answer by any means. Even if you don't like thehouse's answer, and considering this was written in 2009, it's completely non-cross platform. Use thehouse's answer - and if you don't like it then go for something like `target_link_libraries(target "$<$:-pthread>$<$:-pthreads>")` which is at least target-based and doesn't fail on Windows and other platforms. – Ela782 Dec 11 '16 at 17:35
7

target_compile_options solution above is wrong, it won't link the library.

Use:

SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -pthread")

OR

target_link_libraries(XXX PUBLIC pthread)

OR

set_target_properties(XXX PROPERTIES LINK_LIBRARIES -pthread)

ChristianYami
  • 586
  • 1
  • 9
  • 17
WRFan
  • 147
  • 1
  • 3
  • This is brittle/outdated. Use `find_package(Threads)` and `target_link_libraries(my_app PRIVATE Threads::Threads)` instead. – alexchandel Mar 02 '23 at 00:51