yaml-cpp for C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

yaml-cpp for C++: Complete Guide with Examples

\n\n

1. What It Is and Why You Need It

\n\n

yaml-cpp is a lightweight, modern C++ library for parsing and generating YAML files. YAML (YAML Ain't Markup Language) is a human-readable data serialization format commonly used for configuration files, data structure descriptions, CI/CD pipelines (e.g., GitHub Actions, GitLab CI), and data exchange between services. Unlike JSON, YAML supports comments, multi-line strings, anchors, and aliases, making it ideal for configurations.

\n\n

Why use yaml-cpp? If you are writing a C++ application that needs to read configuration files (e.g., server settings, machine learning model parameters, dependency descriptions), yaml-cpp provides a simple and type-safe API. The library fully complies with the YAML 1.2 specification, supports all data types (scalars, sequences, mappings), and allows easy conversion between YAML documents and C++ structures and back.

\n\n

Key advantages: minimal dependencies (only the C++11/14/17 standard library), optional header-only build, Unicode support, custom serialization for user-defined types, and an active community (over 5,000 stars on GitHub).

\n\n

2. Installation

\n\n

yaml-cpp is not installed via language package managers (pip, npm). For C++, system package managers or building from source are used.

\n\n

Linux (Debian/Ubuntu)

\n
sudo apt-get install libyaml-cpp-dev
\n\n

macOS (Homebrew)

\n
brew install yaml-cpp
\n\n

Windows (vcpkg)

\n
vcpkg install yaml-cpp
\n\n

Building from Source (CMake)

\n
git clone https://github.com/jbeder/yaml-cpp.git\ncd yaml-cpp\nmkdir build && cd build\ncmake -DYAML_BUILD_SHARED_LIBS=ON ..\nmake\nsudo make install
\n\n

After installation, link the library in CMakeLists.txt:

\n
find_package(yaml-cpp REQUIRED)\ntarget_link_libraries(my_project yaml-cpp)
\n\n

3. Quick Start — Minimal Working Example

\n\n

Create a file config.yaml:

\n
server:\n  host: "localhost"\n  port: 8080\ndebug: true
\n\n

Read it using yaml-cpp:

\n
#include <iostream>\n#include <yaml-cpp/yaml.h>\n\nint main() {\n    // Load the YAML file\n    YAML::Node config = YAML::LoadFile("config.yaml");\n    \n    // Read values\n    std::string host = config["server"]["host"].as<std::string>();\n    int port = config["server"]["port"].as<int>();\n    bool debug = config["debug"].as<bool>();\n    \n    std::cout << "Host: " << host << "\\n";\n    std::cout << "Port: " << port << "\\n";\n    std::cout << "Debug: " << std::boolalpha << debug << "\\n";\n    \n    return 0;\n}
\n\n

Compilation: g++ -std=c++17 main.cpp -lyaml-cpp -o app

\n\n

4. Main Methods/Functions/Classes

\n\n

4.1. YAML::Node — Main Class for Working with Nodes

\n

Signature: class YAML::Node

\n

Description: Represents any element of a YAML document: a scalar, sequence (array), or mapping (dictionary). Supports iteration, access by key/index, and type checking.

\n
YAML::Node node;\nnode["name"] = "Alice";\nnode["age"] = 30;\nnode["hobbies"].push_back("reading");\nnode["hobbies"].push_back("coding");\n\nstd::cout << node["name"].as<std::string>() << "\\n"; // Alice\nstd::cout << node["hobbies"][0].as<std::string>() << "\\n"; // reading

Recommendations