当您需要指定 CMAKE_MODULE_PATH 时,find_package() 有什么用? [英] What use is find_package() when you need to specify CMAKE_MODULE_PATH?

查看:18
本文介绍了当您需要指定 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)

第一个命令定义了 Find*.cmake 之后 CMake 搜索的位置,我添加了 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.

这通常是怎么做的?我是否应该将SomeLibcmake/目录复制到我的项目中并相对设置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?

推荐答案

Command find_package 有两种模式:Module 模式和 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.

查找项目中的.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.cmake 文件时可能会很有用.

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

Config.cmake 文件位于外部并由 install 生成其他项目的命令(例如Foo).

<package>Config.cmake file located outside and produced by install command of other project (Foo for example).

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

请注意,导入的目标高度可配置.请参阅我的答案.

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

更新

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

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