为 yaml-cpp 库查找正确的 cmake 配置

Finding correct cmake configuration for yaml-cpp library

我尝试使用 yaml-cpp in my project. It took me half an hour to correctly link the library by experimenting with the following names. After I finally stumbled across them in this file,我解决了这个问题:

find_package(yaml-cpp REQUIRED)
include_directories(${YAML_INCLUDE_DIRS})
target_link_libraries(${YAML_CPP_LIBRARIES})

它有效,但我搜索这些的方式似乎很愚蠢。

如何远程找出包含变量的正确名称?可以是YAML_LIBSYAML_LIBRARYYAML_CPP_LIBRARIES,没有标准吧?为大多数 C++ 库确定正确的 cmake 配置的合适方法是什么?

谢谢。

大多数 FindXXX.cmake 脚本的顶部都有 用法描述 (因为 CMake 注释开始 #)。 XXXConfig.cmake(或xxx-config.cmake)脚本也是如此。

命令find_package(XXX) 使用了这样的脚本之一(实际存在的脚本)。因此,在使用此方法发现包之前,请确保您已将描述 "embedded" 读入此类脚本。

在您的情况下,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

所以 find_package(yaml-cpp) 结果的正确用法是

include_directories(${YAML_CPP_INCLUDE_DIRS})
target_link_libraries(<your-target> ${YAML_CPP_LIBRARIES})