jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
5.06k stars 1.82k forks source link

adding yaml-cpp sources to a project #566

Open aoloe opened 6 years ago

aoloe commented 6 years ago

i need to add the yaml-cpp sources to my project.

i could get my code to compile with add_subdirectory() and then target_link_libraries(... yaml-cpp).

but, this way, the full yaml-cpp project is compiled, with its tests and the demo program. there are options to disable them, but i wonder if there is a better way. or if there could be a better way...

i've browsed through the tickets and i got the impression that a few people already asked similar questions over the years but could not find a definitive answer.

i'm still a "cmake" learner, but i would be ready to spend some time and find a solution. ... but i'm happy if people can give me hints (or tell me that my request is nonsensical).

the goal of this ticket is to document the "right way" to include "yaml-cpp" in a project and / or develop a good way...

aoloe commented 6 years ago

for now, i think i've solved the compilation by adding to my main CMakeLists.txt:

set(YAML_CPP_BUILD_TESTS OFF CACHE BOOL "disable yaml tests")
set(YAML_CPP_BUILD_TOOLS OFF CACHE BOOL "disable yaml tools")
set(YAML_CPP_BUILD_CONTRIB OFF CACHE BOOL "disable yaml contrib")

add_subdirectory(third-party/yaml-cpp/)

then, in my sources i can:

#include "yaml-cpp/yaml.h"

and compile it in iwth

target_link_libraries(playground
    yaml-cpp
)

it seems to be working, but i have a bad feeling... i don't know why...

winterheart commented 6 years ago

Best way IMHO is using ExternalProject_Add (rough example):

include(ExternalProject)

ExternalProject_Add(
    yaml_cpp_project
    SOURCE_DIR "${CMAKE_SOURCE_DIR}/third-party/yaml-cpp/"
    INSTALL_DIR "${CMAKE_BINARY_DIR}/prefix"
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)

add_library(yaml-cpp UNKNOWN IMPORTED)
set_target_properties(gmock PROPERTIES
    IMPORTED_LOCATION ${PROJECT_BINARY_DIR}/prefix/lib/${CMAKE_STATIC_LIBRARY_PREFIX}yaml-cpp${CMAKE_STATIC_LIBRARY_SUFFIX}
)

include_directories(SYSTEM "${PROJECT_BINARY_DIR}/prefix/include")

add_dependencies(playground yaml_cpp_project) 

target_link_libraries(playground
   yaml-cpp
)

Includes will be #include <yaml-cpp/yaml.h>

aoloe commented 6 years ago

thanks @winterheart , i will look into this!

lochbrunner commented 6 years ago

@winterheart

But how do you disable YAML_CPP_BUILD_TESTS?

In my case there gtest is used directly by the project. Adding it additional via yaml-cpp causes the errror

... add_library cannot create target "gtest" because another target with the ...

winterheart commented 6 years ago

Just add -DYAML_CPP_BUILD_TESTS=OFF to CMAKE_ARGS in ExternalProject_Add:

ExternalProject_Add(
    yaml_cpp_project
    SOURCE_DIR "${CMAKE_SOURCE_DIR}/third-party/yaml-cpp/"
    INSTALL_DIR "${CMAKE_BINARY_DIR}/prefix"
    CMAKE_ARGS -DYAML_CPP_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)

Then it should be compiles without tests.

winterheart commented 4 years ago

Hello again, there another way to add yaml-cpp as internal dependency based on CMake 3.11 FetchContent module:

include(FetchContent)
FetchContent_Declare(
        yaml-cpp
        GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
        GIT_SHALLOW ON
        GIT_TAG yaml-cpp-0.6.3
    )
FetchContent_GetProperties(yaml-cpp)
if(NOT yaml-cpp_POPULATED)
    message(STATUS "Populating yaml-cpp...")
    FetchContent_Populate(yaml-cpp)
    # Add here options for yaml-cpp building
    set(YAML_CPP_BUILD_TESTS OFF)
    add_subdirectory(${yaml-cpp_SOURCE_DIR} ${yaml-cpp_BINARY_DIR})
    message(STATUS "Done.")
endif()

include_directories(BEFORE SYSTEM ${yaml-cpp_SOURCE_DIR} ${yaml-cpp_BINARY_DIR}/include)
target_link_libraries(${YOUR_TARGET} yaml-cpp)
MarineChap commented 4 years ago

Hello, I just try your solution. Thanks you it was exactly what I was looking for. Except, that tests are not disabled. It has been disabled only when adding this set(YAML_CPP_BUILD_TESTS OFF CACHE BOOL "disable yaml tests") I am new in cmake and compilation. So I am not sure it makes sense...

gris-martin commented 4 years ago

@winterheart The FetchContent snippet worked great for me! Why is the include_directories command needed though? Shouldn't the include directories be set up automatically when linking with the yaml-cpp target?

@MarineChap Hmm, it worked for me using the regular variable. Maybe try and do some message printouts of the variable and see where it changes from OFF to ON when using the regular variable. Both in your CMakeLists.txt and yaml-cpp's. For me it was located here: [build dir]/_deps/yaml-cpp-src/CMakeLists.txt.