问题
I was trying to use 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, I settled for this:
find_package(yaml-cpp REQUIRED)
include_directories(${YAML_INCLUDE_DIRS})
target_link_libraries(${YAML_CPP_LIBRARIES})
It works, but the way I was searching for those seems brainless.
How is it remotely possible to figure out the correct name of the include variables? It could be YAML_LIBS
, YAML_LIBRARY
, YAML_CPP_LIBRARIES
, there is no standard, right? What is the appropriate way to determine the correct cmake config for most c++ libraries?
Thank you.
回答1:
Most of FindXXX.cmake
scripts have usage description at the top of them (as CMake comments started #
). The same is true about XXXConfig.cmake
(or xxx-config.cmake
) scripts.
Command find_package(XXX)
uses one of such scripts (the one which actually exists). So, before using this approach for discover the package, make sure that you have read the description "embedded" into such script.
In your case, yaml-cpp-config.cmake
file (created in the build or in the install directory) contains following description:
# - 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
so proper usage of results of find_package(yaml-cpp)
is
include_directories(${YAML_CPP_INCLUDE_DIRS})
target_link_libraries(<your-target> ${YAML_CPP_LIBRARIES})
来源:https://stackoverflow.com/questions/51036144/finding-correct-cmake-configuration-for-yaml-cpp-library