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

Leave a Comment