为CMake项目创建配置文件 [英] Create config file for CMake project

查看:115
本文介绍了为CMake项目创建配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我拥有的示例项目生成配置CMake文件.我的示例由两个CMake项目组成:一个库,该库在内部使用spdlog并在其界面中使用eigen;一个应用程序,在使用该库.这个想法是学习如何创建Config.cmake文件,以便应用程序可以找到该库.这就是我所拥有的:

I am trying to generate a config CMake file for a sample project I have. My example is made of two CMake projects: a library, which uses spdlog internally and eigen in its interface and an application, which uses the library. The idea is to learn how to create a Config.cmake file so the application can find the library. This is what I have:

cmake_minimum_required(VERSION 3.16)
project(tutorial-4 CXX)

set(CMAKE_CXX_STANDARD 14)

find_package(Eigen3 REQUIRED)

set(LIBRARY_SOURCES
    Lib/lib.cpp)

set(LIBRARY_HEADERS
    Lib/lib.h)

add_library(smalllib-4
    SHARED
    ${LIBRARY_SOURCES}
    ${LIBRARY_HEADERS}
    )

find_package(spdlog REQUIRED)
find_package(Eigen3 REQUIRED)

target_link_libraries(smalllib-4
    PRIVATE spdlog::spdlog
    PUBLIC Eigen3::Eigen)

target_include_directories(smalllib-4 PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  $<INSTALL_INTERFACE:include>
)

set_target_properties(smalllib-4 PROPERTIES PUBLIC_HEADER ${LIBRARY_HEADERS})

install(TARGETS smalllib-4
        EXPORT smalllib
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib/static
        PUBLIC_HEADER DESTINATION include/Lib)

install(EXPORT smalllib NAMESPACE smalllib:: DESTINATION share/smalllib)
include(CMakePackageConfigHelpers)
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/smalllibConfig.cmake
    INSTALL_DESTINATION share/smalllib
    )

install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/smalllibConfig.cmake
    DESTINATION share/smalllib
    )

这成功生成了一个smalllibConfig.cmake,但是当我尝试执行 find_package(需要smalllib)时,出现以下错误:

This generates succesfuly a smalllibConfig.cmake, but when I try to do find_package(smalllib REQUIRED), I get the following error:

CMake Error at CMakeLists.txt:10 (add_executable):
  Target "smallapp" links to target "Eigen3::Eigen" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

发生这种情况是因为在生成的smalllib.cmake中,我有以下内容

This happens because in the generated smalllib.cmake, I have the following

add_library(smalllib::smalllib-4 SHARED IMPORTED)

set_target_properties(smalllib::smalllib-4 PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "Eigen3::Eigen"
)

但此处未定义 Eigen3 :: Eigen .

本征是库的依赖项,因此将其添加到应用程序似乎不是使用现代CMake的正确方法.为CMake创建配置文件的通用方法是什么?

Eigen is a dependency of the library, so adding it to the application doesn't seem to be the correct way to go using modern CMake. What is the generic way to create my config files for CMake?

推荐答案

smalllib 包的配置文件应定义 Eigen3 :: Eigen 目标.最直接的方法是对Eigen3使用 find_dependency 宏作为您可以在包本身的 CMakeLists.txt 中使用 find_package :

The config file for smalllib package should define Eigen3::Eigen target. The most direct approach is to use find_dependency macro for Eigen3 as you use find_package in the CMakeLists.txt for the package itself:

Config.cmake.in (此文件用作生成的配置文件的模板):

Config.cmake.in (this file is used as a template for the resulted config file):

# Get definition of 'find_dependency' macro
include(CMakeFindDependencyMacro)
# Find Eigen3. This will create 'Eigen3::Eigen' target
find_dependency(Eigen3)

# Include CMake-generated config file for the 'smalllib' target
# which uses 'Eigen3::Eigen' target.
include(${CMAKE_CURRENT_LIST_DIR}/smalllib.cmake)

有关包装的CMake文档.

这篇关于为CMake项目创建配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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