Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correctly set CMake variables when adding yaml-cpp to existing project

I've added the yaml-cpp git repository as a submodule and add it to my CMake project using add_subdirectory.

Everything's fine but I have to set YAML_CPP_INCLUDE_DIR and YAML_CPP_LIBRARIES manually to use them for my own targets.

Because there is a file yaml-cpp-config.cmake (generated in the build folder) setting these variables I tried to just include it:

include("${CMAKE_BINARY_DIR}/yaml-cpp/yaml-cpp-config.cmake")

but then I get:

CMake Error at /bla/bla/build/yaml-cpp/yaml-cpp-config.cmake:11 (include):
  The file

/bla/bla/aml-cpp/yaml-cpp-targets.cmake

  was generated by the export() command.  It may not be used as the argument
  to the include() command.  Use ALIAS targets instead to refer to targets by
  alternative names.

I really don't understand this message. How would I provide my targets with the yaml-cpp include directories and libraries without having to set a hard coded variable?

I'm not searching for a way to correctly include() the file in case it doesn't have to be done. I'm just interested in how I should provide the desired information to my targets.

Unfortunately yaml-cpp seems to not make use of target_include_directories() which would set the include directories automatically where needed.

like image 384
frans Avatar asked May 13 '16 08:05

frans


1 Answers

From description of export command:

Create a file <filename> that may be included by outside projects to import targets from the current project’s build tree.

Note to "outside" word: this is why you get the error message while trying to include the file from the same project, which issues export command.

Correct way to use yaml-cpp-config.cmake file would be building yaml-cpp outside of your project. For example, you may use ExternalProject_Add in conjunction with execute_process for build yaml-cpp as part of configuration stage of your project, see more about this approach here.

Then you may include given file to your project with find_package:

find_package(yaml-cpp PATHS <yaml-cpp-build-dir>)

Note, that yaml-cpp-config.cmake in binary directory describes build state of yaml-cpp project.

If you want to install libraries/executables from your project, you are better to install yaml-cpp, and include corresponded file from its installation directory:

find_package(yaml-cpp PATHS <yaml-cpp-install-dir>)
like image 199
Tsyvarev Avatar answered Sep 27 '22 21:09

Tsyvarev