MantisBT - CMake
View Issue Details
0014767CMakeModulespublic2014-02-20 13:322015-04-06 09:07
zub 
Rolf Eike Beer 
normalminoralways
closedfixed 
linux
CMake 2.8.12.2 
 
0014767: FindThreads.cmake prefers -lpthread over -pthread with GCC
With gcc on Linux, FindThreads.cmake finds out that "-lpthread" works and sticks to that.

Reading gcc man page, I think "-pthread" should be preferred, as it does the right thing:

-pthread Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

Too bad that the actual gcc doc doesn't say much about -pthread: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16519 [^]
Build a simple project that includes FindThreads.
The same issue has been brought up before: http://www.cmake.org/pipermail/cmake/2012-December/052963.html [^]
No tags attached.
child of 0014049closed Rolf Eike Beer FindThreads.cmake: several small bugs 
Issue History
2014-02-20 13:32zubNew Issue
2014-02-21 08:26Brad KingNote Added: 0035164
2014-10-06 15:46Rolf Eike BeerRelationship addedchild of 0014049
2014-10-06 15:54Rolf Eike BeerNote Added: 0036986
2014-10-06 15:54Rolf Eike BeerAssigned To => Rolf Eike Beer
2014-10-06 15:54Rolf Eike BeerStatusnew => resolved
2014-10-06 15:54Rolf Eike BeerResolutionopen => fixed
2015-04-06 09:07Robert MaynardNote Added: 0038419
2015-04-06 09:07Robert MaynardStatusresolved => closed

Notes
(0035164)
Brad King   
2014-02-21 08:26   
The FindThreads module is one of the oldest modules in CMake and pre-dates the modern conventions for how to report its results. It needs work to populate the typical result variables as pointed out in Marcel's post from Dec 2012 that you linked.

Currently the module reports the *libraries* that are needed. Even if the "-pthread" flag were reported as a "library" in CMAKE_THREAD_LIBS_INIT the application would still only use it with target_link_libraries. Then the flag would be used only when linking and not when compiling so the preprocessor definitions would still not appear. AFAIK -pthread and -lpthread are equivalent when the compiler front-end is used only to drive linking and no compilations.

If the module were modernized to report modern result variables then one of them could be the compilation flags needed to use threads. Then it could include -pthread.
(0036986)
Rolf Eike Beer   
2014-10-06 15:54   
Should all be fine now:

  http://www.cmake.org/gitweb?p=cmake.git;a=commitdiff;h=b838deb2e9627bf2d607de300f4a9ca287baec38 [^]

These changes are in next now and will hopefully show up either in 3.1 or 3.2.
(0038419)
Robert Maynard   
2015-04-06 09:07   
Closing resolved issues that have not been updated in more than 4 months.