David
David

Reputation: 10192

yaml-cpp undefined reference with CMake

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.

Upvotes: 1

Views: 2261

Answers (1)

Jesse Beder
Jesse Beder

Reputation: 34044

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.

Upvotes: 1

Related Questions