9

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.

3
  • RTFM ;-): By reading the documentation of Findyaml-cpp and if that is not enough by inspecting the source of Findyaml-cpp in CMAKE_MODULE_PATH Commented Jun 26, 2018 at 6:30
  • 1
    Or by looking at the cmake.in file: github.com/jbeder/yaml-cpp/blob/master/yaml-cpp-config.cmake.in Commented Jun 26, 2018 at 6:30
  • 1
    So is this a recipe? When installing a new library look for the .cmake.in file? Commented Jun 26, 2018 at 6:42

1 Answer 1

22

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_DIR})
target_link_libraries(<your-target> ${YAML_CPP_LIBRARIES})
Sign up to request clarification or add additional context in comments.

1 Comment

All I need. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.