Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

c++ - How to get CMake to recognize pthread on Ubuntu?

If I compile on the command-line with g++ directly, I can see everything I need is there:

$ g++ -pthread test.cpp
$ ldd a.out
    linux-vdso.so.1 =>  (0x00007fffd05b3000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f4a1ba8d000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4a1b870000)
    ...more...

Then I try to create a simple cmake file for this 5-line test app:

$ cat CMakeLists.txt 
PROJECT ( Test CXX )
CMAKE_MINIMUM_REQUIRED ( VERSION 2.8 )
FIND_PACKAGE ( Threads REQUIRED )
ADD_EXECUTABLE ( test test.cpp )
TARGET_LINK_LIBRARIES ( test ${CMAKE_THREAD_LIBS_INIT} )

However, I cannot figure out why CMake doesn't find what it needs to use for Threads:

$ cd build/
$ cmake ..
CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (MESSAGE):
  Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
  /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:288 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-2.8/Modules/FindThreads.cmake:166 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:4 (FIND_PACKAGE)
-- Configuring incomplete, errors occurred!
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...