1

I am trying to load YAML-data to c++ in a setup with cmake and gcc (using Ubuntu), but I get an error that I cannot resolve.

So far I have done the following:

cloned the yaml-cpp repo, created build, used cmake .. and make to build the library and copied the files from Downloads/yaml-cpp/build/include/yaml-cpp/ to myproject/include/yaml-cpp/.

Furthermore, I have a minimum working example that reproduces the error as follows:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
project(YAML_TEST)

set(CMAKE_CXX_STANDARD 11)
include_directories("include")

add_library(YAML_LIB include/yaml-cpp/yaml.h )
set_target_properties(YAML_LIB PROPERTIES LINKER_LANGUAGE CXX)

set(SOURCE_FILES main.cpp)
add_executable(YAML_TEST ${SOURCE_FILES})
target_link_libraries(YAML_TEST YAML_LIB)

main.cpp:

#include <iostream>
#include <string.h>
#include "include/yaml-cpp/yaml.h"

int main() {
    YAML::Node config = YAML::LoadFile("test.yaml");
    std::cout << "tag: " << config["tag"].as<std::string>() << "\n";

    return 0;
}

And I also have a small yaml-file (test.yaml), that contains:

tag: "This is a text"
category:
  anothertag: 123

However, if I try to compile the project, I get the error

/home/david/Desktop/myproject/main.cpp:6: undefined reference to `YAML::LoadFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' ...

I guess the project doesn't properly find the yaml-cpp library. But I am not able to resolve this.

1 Answer 1

1

The line

add_library(YAML_LIB include/yaml-cpp/yaml.h )

will try to make a library using only that header file, which won't have any of the symbols that make up yaml-cpp. You need to build the library and install it.

Sign up to request clarification or add additional context in comments.

2 Comments

That pointed me in the right direction. I ran make instead of make install.
For someone who is stuck with the same issues I had, after make install, you also have to link the yaml-cpp in cmake using target_link_libraries(YAML_TEST yaml-cpp)

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.