This question already has an answer here:

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 ${CMAKE_C_FLAGS} "-pthread"), but it doesn't look like a correct solution to me.

share|improve this question

marked as duplicate by Ciro Santilli 华涌低端人口 六四事件 法轮功, il_raffa, ZdaR, Andy, LSerni Mar 5 '17 at 19:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

8  
You are marking this 2011 post as a duplicate of a 2013 post. Please! – Henk van Boeijen Mar 5 '17 at 15:49
2  
@HenkvanBoeijen, exactly, also this question has better answers – maxschlepzig May 1 '17 at 22:18
1  
@maxschlepzig That's highly opinion based. I think good quality questions with accepted answers and a decent amount of upvotes should never be closed. – Henk van Boeijen May 1 '17 at 23:28
    
@HenkvanBoeijen, I am referring to this 2011 question. – maxschlepzig May 2 '17 at 7:34
up vote 37 down vote accepted

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} )
share|improve this answer
3  
But the point is to add this "-pthread" option during compilation, not linking. – Tomasz Grobelny Mar 23 '11 at 13:07
    
Oh, I see what you mean. I do not believe there is a special way to do that. On most platforms -pthread adds -D_REENTRANT and -lpthread. You could add -D_REENTRANT fairly easily (btw this is a noop on any modern platform I have ever checked on). The real question though is what type of portability are you trying to get. The -pthead option is a gcc option that may not exist for other compilers. If you want to support the most OS platforms possible, with gcc, then what you are currently doing is the way to go. If you want to support multiple compilers on a few platforms use the above. – ltc Mar 23 '11 at 19:39
    
Personally I don't care about this option. The problem is that Boost.Interprocess cares. Without it the compiler exits on an #error directive that clearly states that -pthread is needed (there were alternatives given for different systems - cannot quote it now as I don't have access to that system at the moment). – Tomasz Grobelny Mar 23 '11 at 21:18
    
Ahh. Well there is no way that boost can know if you actually added the -pthread option to the gcc command line. What it can know is if the _REENTRANT macro is set. I took a look and that is indeed what it does to check if the -pthread option is set. At any rate it looks like given your requirements the solution that you already implemented is fine. I would just leave it the way it is. – ltc Mar 24 '11 at 8:10

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

share|improve this answer

How about the following:

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
if(CMAKE_USE_PTHREADS_INIT)
    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread")
elseif(...)
    ...
endif()
add_executable( test test.cpp )
target_link_libraries( test ${CMAKE_THREAD_LIBS_INIT} )
share|improve this answer
    
The line set(CMAKE_THREAD_PREFER_PTHREAD TRUE) rescued me from hours of trying to get beignet compile (Debian Jessie) – Jealie Oct 20 '14 at 17:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.