jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
4.91k stars 1.77k forks source link

Cannot compile MWE of a yaml parser #1246

Closed aburlot closed 7 months ago

aburlot commented 7 months ago

Hi, 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
        std::string name = config["name"].as<std::string>();
        int age = config["age"].as<int>();
        std::string city = config["city"].as<std::string>();

        // Print parsed values
        std::cout << "Name: " << name << std::endl;
        std::cout << "Age: " << age << std::endl;
        std::cout << "City: " << city << 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
city: Paris

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

/usr/bin/ld : /tmp/ccgl05V0.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/ccgl05V0.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/ccgl05V0.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

Am I doing something bad? I think the wiki lacks some complete examples.

jbeder commented 7 months ago

Stack Overflow

aburlot commented 7 months ago

On SO, if you have some time