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.