为什么我的 <package>-config.cmake <package>_include_dir 和 <package>_librairies 是空的

Why my <package>-config.cmake have <package>_include_dir and <package>_librairies empty

我正在尝试为我的项目制作一个跨平台的 CMake(Windows 和 Linux)。 我需要使用外部库 (yaml-cpp)。在 Linux,我只需要做一个 apt get 和 use find_package。但是在 Windows 上,我需要附加 CMAKE_MODULE_PATH 以便我的程序找到 yaml-cpp-config.cmake

所以我首先安装带有 CMake GUI 3.16 和 mingw32 (mingw32-make install) 的 yaml-cpp (https://github.com/jbeder/yaml-cpp)。

我已经在一个 hello world 项目上试用了这个库,它工作正常。

cmake_minimum_required(VERSION 3.1)

project (yaml_test)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

message (STATUS "Yaml-cpp include = $ENV{YAML_CPP_INCLUDE_DIR}")
message (STATUS "Yaml-cpp library = $ENV{YAML_CPP_LIBRARIES}")

include_directories ($ENV{YAML_CPP_INCLUDE_DIR})

add_executable(yaml_test main.cpp)

target_link_libraries(yaml_test $ENV{YAML_CPP_LIBRARIES})

但是现在,我想在我的项目中包含该库并使用 find_package。但是 yaml-cpp-config.cmake 看起来像这样:

# - Config file for the yaml-cpp package
# It defines the following variables
#  YAML_CPP_INCLUDE_DIR - include directory
#  YAML_CPP_LIBRARIES    - libraries to link against

# Compute paths
get_filename_component(YAML_CPP_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(YAML_CPP_INCLUDE_DIR "")

# Our library dependencies (contains definitions for IMPORTED targets)
include("${YAML_CPP_CMAKE_DIR}/yaml-cpp-targets.cmake")

# These are IMPORTED targets created by yaml-cpp-targets.cmake
set(YAML_CPP_LIBRARIES "")

YAML_CPP_INCLUDE_DIRYAML_CPP_LIBRARIES变量为空,即使CMake找到了yaml-cpp-config.cmake,也不行。那么我在yaml-cpp的安装中缺少了什么?我应该手动设置路径吗?

缺少 YAML_CPP_INCLUDE_DIRYAML_CPP_LIBRARIES 变量的定义是 yaml-cpp 项目的问题,该项目已被报告 here

不要使用此配置文件中描述的变量,而是使用目标 yaml-cpp:

add_executable(yaml_test main.cpp)
# This provides both include directories and libraries.
target_link_libraries(yaml_test yaml-cpp)

与 IMPORTED 目标(yaml-cpp 在这种情况下)的链接被称为 CMake "modern way"。