I'm using yaml-cpp library recently. I follow the config file in "yaml-cpp-config.cmake.in" which content is
# - 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
# Compute paths
get_filename_component(YAML_CPP_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(YAML_CPP_INCLUDE_DIR "@CONFIG_INCLUDE_DIRS@")
# Our library dependencies (contains definitions for IMPORTED targets)
include("${YAML_CPP_CMAKE_DIR}/yaml-cpp-targets.cmake")
# These are IMPORTED targets created by yaml-cpp-targets.cmake
set(YAML_CPP_LIBRARIES "@EXPORT_TARGETS@")
According line 8 and line 14 (or the comment section), I edited my CMakeLists.txt
:
cmake_minimum_required(VERSION 3.5)
project(yaml_test)
find_package(yaml-cpp)
include_directories(${YAML_CPP_INCLUDE_DIRS})
add_executable(yaml_test src/test.cpp)
target_link_libraries(yaml_test ${YAML_CPP_LIBRARIES})
and test.cpp
:
#include <yaml-cpp/yaml.h>
int main()
{
YAML::Node node = YAML::LoadFile("test.yaml");
return 0;
}
and I got
CMakeFiles/yaml_test.dir/src/test.cpp.o: In function `main':
test.cpp:(.text+0xf9): undefined reference to `YAML::LoadFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/yaml_test.dir/build.make:94: recipe for target 'yaml_test' failed
make[2]: *** [yaml_test] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/yaml_test.dir/all' failed
make[1]: *** [CMakeFiles/yaml_test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I found a solution from undefined reference to YAML::LoadFile
changed my CMakeLists.txt
to
cmake_minimum_required(VERSION 3.5)
project(yaml_test)
find_package(yaml-cpp)
include_directories(${YAMLCPP_INCLUDE_DIRS})
add_executable(yaml_test src/test.cpp)
target_link_libraries(yaml_test ${YAMLCPP_LIBRARIES})
And I can build my code succesfully.
My question is how can I tell the right include_directories
and target_link_libraries
straight from yaml-cpp package's CMakeLists.txt
or config file?