如果仍然需要指定CMAKE_MODULE_PATH,find_package()有什么用? [英] What use is find_package() if you need to specify CMAKE_MODULE_PATH anyway?

查看:151
本文介绍了如果仍然需要指定CMAKE_MODULE_PATH,find_package()有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CMake来构建跨平台的构建系统。现在,该软件具有一些依赖性。我自己编译它们并将其安装在系统上。

I'm trying to get a cross-plattform build system working using CMake. Now the software has a few dependencies. I compiled them myself and installed them on my system.

一些已安装的示例文件:

Some example files which got installed:

-- Installing: /usr/local/share/SomeLib/SomeDir/somefile
-- Installing: /usr/local/share/SomeLib/SomeDir/someotherfile
-- Installing: /usr/local/lib/SomeLib/somesharedlibrary
-- Installing: /usr/local/lib/SomeLib/cmake/FindSomeLib.cmake
-- Installing: /usr/local/lib/SomeLib/cmake/HelperFile.cmake

现在CMake具有 find_package()打开 Find * .cmake 文件并在系统上的库后进行搜索,并定义一些变量,例如 SomeLib_FOUND 等。

Now CMake has a find_package() which opens a Find*.cmake file and searches after the library on the system and defines some variables like SomeLib_FOUND etc.

我的CMakeLists.txt包含以下内容:

My CMakeLists.txt contains something like this:

set(CMAKE_MODULE_PATH "/usr/local/lib/SomeLib/cmake/;${CMAKE_MODULE_PATH}")
find_package(SomeLib REQUIRED)

第一个命令定义CMake在 Find * .cm之后搜索的位置ake ,然后添加了 SomeLib 目录,在其中可以找到 FindSomeLib.cmake ,因此 find_package()可以像预期的那样工作

The first command defines where CMake searches after the Find*.cmake and I added the directory of SomeLib where the FindSomeLib.cmake can be found, so find_package() works as expected.

但这有点奇怪,因为其中一个 find_package()存在的原因是要摆脱非跨平台的硬编码路径。

But this is kind of weird because one of the reasons why find_package() exists is to get away from non-cross-plattform hard coded paths.

如何通常这样做吗?我是否应该将 SomeLib cmake / 目录复制到我的项目中并设置 CMAKE_MODULE_PATH

How is this usually done? Should I copy the cmake/ directory of SomeLib into my project and set the CMAKE_MODULE_PATH relatively?

推荐答案

命令 find_package 有两种模式:模块模式和 Config 模式。当您实际需要 Config 模式时,您试图
使用 Module 模式。

Command find_package has two modes: Module mode and Config mode. You are trying to use Module mode when you actually need Config mode.

查找< package> .cmake 文件位于在您的项目中。像这样的东西:

Find<package>.cmake file located within your project. Something like this:

CMakeLists.txt
cmake/FindFoo.cmake
cmake/FindBoo.cmake

CMakeLists.txt 内容:

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
find_package(Foo REQUIRED) # FOO_INCLUDE_DIR, FOO_LIBRARIES
find_package(Boo REQUIRED) # BOO_INCLUDE_DIR, BOO_LIBRARIES

include_directories("${FOO_INCLUDE_DIR}")
include_directories("${BOO_INCLUDE_DIR}")
add_executable(Bar Bar.hpp Bar.cpp)
target_link_libraries(Bar ${FOO_LIBRARIES} ${BOO_LIBRARIES})

请注意 CMAKE_MODULE_PATH 具有较高的优先级,当您需要重写标准 Find< package> .cmake 文件时,可能会很有用。

Note that CMAKE_MODULE_PATH has high priority and may be usefull when you need to rewrite standard Find<package>.cmake file.

外部的$ c>文件,该文件由其他$ install
命令生成r项目(例如 Foo )。

foo 库:

> cat CMakeLists.txt 
cmake_minimum_required(VERSION 2.8)
project(Foo)

add_library(foo Foo.hpp Foo.cpp)
install(FILES Foo.hpp DESTINATION include)
install(TARGETS foo DESTINATION lib)
install(FILES FooConfig.cmake DESTINATION lib/cmake/Foo)

配置文件的简化版本:

> cat FooConfig.cmake 
add_library(foo STATIC IMPORTED)
find_library(FOO_LIBRARY_PATH foo HINTS "${CMAKE_CURRENT_LIST_DIR}/../../")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION "${FOO_LIBRARY_PATH}")

默认项目安装在 CMAKE_INSTALL_PREFIX 目录:

> cmake -H. -B_builds
> cmake --build _builds --target install
-- Install configuration: ""
-- Installing: /usr/local/include/Foo.hpp
-- Installing: /usr/local/lib/libfoo.a
-- Installing: /usr/local/lib/cmake/Foo/FooConfig.cmake



配置模式(使用)



使用 find_package(... CONFIG)包括 FooConfig.cmake ,导入目标为 foo

Config mode (use)

Use find_package(... CONFIG) to include FooConfig.cmake with imported target foo:

> cat CMakeLists.txt 
cmake_minimum_required(VERSION 2.8)
project(Boo)

# import library target `foo`
find_package(Foo CONFIG REQUIRED)

add_executable(boo Boo.cpp Boo.hpp)
target_link_libraries(boo foo)
> cmake -H. -B_builds -DCMAKE_VERBOSE_MAKEFILE=ON
> cmake --build _builds
Linking CXX executable Boo
/usr/bin/c++ ... -o Boo /usr/local/lib/libfoo.a

请注意,导入的目标可以高度进行配置。请参阅我的 answer

Note that imported target is highly configurable. See my answer.

更新 >

  • Example

这篇关于如果仍然需要指定CMAKE_MODULE_PATH,find_package()有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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