如何让CMake在Ubuntu上识别pthread? [英] How to get CMake to recognize pthread on Ubuntu?

查看:2128
本文介绍了如何让CMake在Ubuntu上识别pthread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我直接在命令行上编译g ++,我可以看到我需要的一切是:

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

然后我尝试为这个5行测试应用程序创建一个简单的cmake文件:

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

但是,我不知道为什么CMake找不到需要使用主题

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!


推荐答案

我可能失去了2个小时。这里是解决方案:

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

CMake使用短'C'应用程序测试/尝试的东西。如果CMakeLists.txt声明C ++用于项目,没有也列出C,那么这些短路测试中的一些错误地失败,并且cmake然后认为那些东西没有找到。

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.

解决方案是从这里改变CMakeLists的第一行:

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

PROJECT ( Test CXX )

...以包含C作为语言:

...to include C as a language:

PROJECT ( Test C CXX )

c> build ,重新创建它,然后一切工作:

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

rm -rf build
mkdir build
cd build
cmake ..

这篇关于如何让CMake在Ubuntu上识别pthread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆