0

I am using Ubuntu 20.04 with libyaml-cpp-dev (version 0.6.2-4ubuntu1).

I try to compile this simple example

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

int main() {
    try {
        // Load YAML file
        YAML::Node config = YAML::LoadFile("example.yaml");

        // Access values in YAML file
        auto name = config["name"].as<std::string>();
        auto age = config["age"].as<int>();

        // Print parsed values
        std::cout << "Name: " << name << std::endl;
        std::cout << "Age: " << age << std::endl;

    } catch (const YAML::Exception& e) {
        std::cerr << "Error parsing YAML: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

with this example.yaml file

name: Gandalf The Grey
age: 20

The compile command is g++ yaml-parser.cpp -std=c++11 -lyaml-cpp -Wall -Werror -pedantic -o yaml-parser.x and I obtain the following error:

/usr/bin/ld : /tmp/cczXm7sC.o : dans la fonction « YAML::Node::Scalar[abi:cxx11]() const » :
yaml-parser.cpp:(.text._ZNK4YAML4Node6ScalarB5cxx11Ev[_ZNK4YAML4Node6ScalarB5cxx11Ev]+0x7a) : référence indéfinie vers « YAML::detail::node_data::empty_scalar[abi:cxx11]() »
/usr/bin/ld : /tmp/cczXm7sC.o : dans la fonction « YAML::detail::node& YAML::detail::node_data::get<char [5]>(char const (&) [5], std::shared_ptr<YAML::detail::memory_holder>) » :
yaml-parser.cpp:(.text._ZN4YAML6detail9node_data3getIA5_cEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data3getIA5_cEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0xd3) : référence indéfinie vers « YAML::detail::node_data::convert_to_map(std::shared_ptr<YAML::detail::memory_holder> const&) »
/usr/bin/ld : /tmp/cczXm7sC.o : dans la fonction « YAML::detail::node& YAML::detail::node_data::get<char [4]>(char const (&) [4], std::shared_ptr<YAML::detail::memory_holder>) » :
yaml-parser.cpp:(.text._ZN4YAML6detail9node_data3getIA4_cEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data3getIA4_cEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0xd3) : référence indéfinie vers « YAML::detail::node_data::convert_to_map(std::shared_ptr<YAML::detail::memory_holder> const&) »
collect2: error: ld returned 1 exit status

I tried compiling it, narrowing the problem. It seems it's the config["name"] that blocks compilation. Am I doing something wrong? As already mentionned here, the wiki lacks a complete example.

3
  • 1
    1. You might try using a more recent version than 0.6.2 Commented Nov 23, 2023 at 13:53
  • 1
    2. This usually means your installation wasn’t correct, not your code. Your code looks fine. Check if you have two versions installed or something. Commented Nov 23, 2023 at 13:54
  • Thanks! I just checked in another computer with Ubuntu 22.04 and it works well. I'll have a look on the first one. I tried to use the git repo and might have forgot to remove something. Commented Nov 23, 2023 at 19:12

0

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.