CMake找不到所需的库 [英] CMake cannot find a required library

查看:926
本文介绍了CMake找不到所需的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的CMakeLists.txt:

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
include_directories(/usr/include/gtest)

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests gtest.cpp)
target_link_libraries(runTests /usr/lib/gtest pthread)

当运行cmake时,我得到以下错误:

When running cmake I get the following error:

michael@michaelFriko:~/workspace/gtest/src$ cmake CMakeLists.txt
CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (message):
  Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY)
Call Stack (most recent call first):
  /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:291 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-2.8/Modules/FindGTest.cmake:150 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:8 (find_package)

如何解决此问题?

推荐答案

你倒退了。 find_package 调用应该为您找到gtest库的位置。您不需要再手动指定包含和库路径:

You got it backwards. The find_package call is supposed to find the location of the gtest library for you. You won't need to manually specify the include and library paths anymore:

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests my_test.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread)

查看 FindGTest.cmake 在您的CMake模块目录中。

Take a look at the FindGTest.cmake in your CMake modules directory for details.

出现错误信息的原因是 find_package(GTest REQUIRED)无法在您的系统上找到gtest。使用 REQUIRED 参数,如果无法找到库(这里实际上是正确的做法),请求CMake立即失败。

The problem why you got the error message is that find_package(GTest REQUIRED) is unable to find gtest on your system. With the REQUIRED parameter, you requested CMake to fail immediately if the library cannot be found (which is actually the right thing to do here).

所以你需要做的是提供 FindGTest 和找到你的库的方法。不幸的是,没有标准的方法来做到这一点,因为找到一个库需要的信息从库到库变化。因此,您必须检查查找脚本的来源。

So what you need to do is provide FindGTest with the means to locate your library. Unfortunately, there is no standard way to do this, as the information needed to find a library varies from library to library. So you will have to check out the source of the find script.

这将告诉您 FindGTest 依赖于环境变量 GTEST_ROOT 找到库。将该环境变量设置为您的gtest安装的路径,重新运行CMake,你应该很好。

This will tell you that FindGTest relies on the environment variable GTEST_ROOT to find the library. Set that environment variable to the path of your gtest installation, re-run CMake and you should be fine.

如果你的安装布局不同于 FindGTest 预期,您可能需要编写自己的查找脚本。 CMake提供的查找脚本通常是相当不错的,但有时他们只是不在某些平台上开箱即用。如果你可以想出一个补丁,为你的平台增加支持,通常没有问题,让它与官方CMake分布集成。

If your installation's layout differs from the one that FindGTest expects, you may have to write your own find script instead. The find scripts that ship with CMake are usually quite good, but sometimes they just don't work on certain platforms out-of-the-box. If you can come up with a patch that adds support for your platform, it is usually no problem to get it integrated with the official CMake distribution.

请注意,如果你打算构建gtest自己(而不是使用您的操作系统提供的二进制文件)使用find脚本不是最好的主意。您应改用汇入的目标

Note that if you intend to build gtest yourself (instead of using the binaries provided by your operating system) using the find script is not the best idea in the first place. You should use an imported target instead.

这篇关于CMake找不到所需的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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