5

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.

frans
  • 8,868
  • 11
  • 58
  • 132
  • This is a developer warning and can be controlled with CMake policy [CMP0024](https://cmake.org/cmake/help/latest/policy/CMP0024.html). The reasoning for the warning: "CMake 2.8.12 and lower allowed use of the include() command with the result of the export() command. This relies on the assumption that the export() command has an immediate effect at configure-time during a cmake run. ... Future refactoring will change the effect of the export() command to be executed at generate-time. Use ALIAS targets instead in cases where the goal is to refer to targets by another name". – Florian May 13 '16 at 08:54

1 Answers1

5

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>)
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • That's not what I was hoping to hear but probably it's the correct approach :) – frans May 13 '16 at 11:33
  • What should I include as the libraries? I tried the following but is did not work: set(YAML_CPP_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/gtsam/3rdparty/yaml-cpp/include/yaml-cpp ${CMAKE_CURRENT_SOURCE_DIR}/gtsam/3rdparty/yaml-cpp/include ${CMAKE_CURRENT_SOURCE_DIR}/gtsam/3rdparty/yaml-cpp/src ${CMAKE_CURRENT_SOURCE_DIR}/gtsam/3rdparty/yaml-cpp) find_package(yaml-cpp PATHS ${CMAKE_CURRENT_SOURCE_DIR}/gtsam/3rdparty/yaml-cpp/build) include_directories(${YAML_CPP_INCLUDE_DIRS}) – Yonatan Simson Sep 28 '16 at 11:00
  • It is described in `YAMLCPPConfig.cmake` file which variables are set as a result of `find_package(yaml-cpp)`. Please, create new question post and describe your problem there, comments are not suitable for new questions. – Tsyvarev Sep 28 '16 at 11:45
  • I've encountered this problem as well. So from what I understand the easy solution is still to hard-code the paths? The actual "solutions" seem highly stupid. – tambre Nov 26 '16 at 18:34
  • @tambre: Using `add_subdirectory(yaml-cpp)` means that you want to treat `yaml-cpp` as a *collection of targets and files* (with hardcoded paths). If you want to treat `yaml-cpp` as *unified thing*, you need to **install it**, either within your project (`ExternalProject_Add` in conjunction with `execute_process`) or as a *prerequisite* to your project. After installation, you may use `find_package(yaml-cpp)`. – Tsyvarev Nov 28 '16 at 05:26
  • 1
    @Tsyvarev I see. Might be worth noting that this can be fixed easily by disabling the yaml-cpp-targets script, which specifies yaml-cpp as an IMPORTED target. I also created an issue regarding this: https://github.com/jbeder/yaml-cpp/issues/439 – tambre Nov 29 '16 at 17:40