How to Get Cmake to Recognize Pthread on Ubuntu

How to get CMake to recognize pthread on Ubuntu?

Oh, this was was a pain! I probably lost 2 hours on this. Here is the solution:

CMake uses short 'C' applications to test/try things. If the CMakeLists.txt states that C++ is used for the project, without also listing C, then some of those shorts tests incorrectly fail, and cmake then thinks those things aren't found.

The solution was to change the first line of CMakeLists from this:

PROJECT ( Test CXX )

...to include C as a language:

PROJECT ( Test C CXX )

Then delete build, recreate it, and everything then works:

rm -rf build
mkdir build
cd build
cmake ..

How do I force cmake to include -pthread option during compilation?

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} )

Building error using cmake: cannot find -lpthreads

The problem was happening when running cmake. Though, in this case cmake was not the problem the error was silent and the -lpthreads related error/warning was the only thing being written to the cmake error log file, although that was not causing any issue.
I've done a minimal version of the cmakelists.txt and started testing it line by line until I found which package was causing it to stop: finally I found it was a version mismatch...

Hint: search for the actual error message

Typically you'd look for the last error message. However, this (often useful) strategy in such cases leads astray.

What you are looking at is the CMakeCache.txt, the CMakeOutput.log or the CMakeError.log. How comes? When some of the macros or tests in the configure phase fails, CMake "helpfully" dumps these files to the output. Unfortunately, these files can be thousands of lines long, and typically contain lots of "*** Error: xyz" entries, for various configure checks. The one for "-lpthreads" just accidentally happened to be the last one in the log...

Solution: go through the log from the top, identify the section with the configure checks, find the last configure check prior to the point, where CMake identifies failure and dumps its logs. You might also try so search for the text "Configuring incomplete, errors occurred!"

Typically you'll either find a very precise actual error message there, or at least you find the name / path of the macro or function called last, and this allows you to pinpoint down what actually went wrong.

cmake does not consider -pthread

I integrated Google Test framework in my own project and this CMake file is working.

# Google C++ Testing Framework
# https://code.google.com/p/googletest/
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

if(WIN32)
# GTest is static compiled by default
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
endif()

enable_testing()
add_executable(ThreadTest main_test.cpp)
target_link_libraries(ThreadTest ${GTEST_BOTH_LIBRARIES})

CMake warning "Could NOT find Threads" is related to a bug:
CMake failing to detect pthreads due to warnings

CMake undefined reference to `pthread_create` in Github Action Ubuntu image

If you read the build log carefully

/usr/bin/ld: CMakeFiles/GenerateAudioModelTest.dir/__/src/GenerateAudioModel.cpp.o: in function `GenerateAudioModel::GenerateModelFromFile()':
GenerateAudioModel.cpp:(.text+0x27aa): undefined reference to `pthread_create'

You notice the error has happened while linking the target GenerateAudioModelTest that is located in the directory test and CMakeLists.txt there does not have the compiler flags you shown. Just add -pthread in test/CMakeLists.txt.


This is a bad idea.

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

Use

target_compile_options(GenerateAudioModelTest PRIVATE -pthread)

See What is the modern method for setting general compile flags in CMake?

CMake failing to detect pthreads due to warnings

I believe this is CMake bug 15058 which I just reported.

The test that CMake is using to check the include file uses an old-style C function definition. If -Wold-style-definition -Werror is in effect, gcc will barf on this.

I included a patch in the bug report linked above, but for a quick fix, find the file Modules/CheckIncludeFiles.cmake in your CMake installation (possibly in /usr/share/cmake or similar), find the line

  "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n\nint main(){return 0;}\n")

and change int main() to int main(void).



Related Topics



Leave a reply



Submit