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.

1
  • This is a developer warning and can be controlled with CMake policy CMP0024. 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". Commented May 13, 2016 at 8:54

1 Answer 1

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>)
Sign up to request clarification or add additional context in comments.

6 Comments

That's not what I was hoping to hear but probably it's the correct approach :)
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})
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.
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.
@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: github.com/jbeder/yaml-cpp/issues/439
|

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.