yaml-cpp

Library

jbeder/yaml-cpp

C++ library for parsing and emitting YAML 1.2.

Overview

yaml-cpp is a C++ library that provides YAML parsing and emitting capabilities according to the YAML 1.2 specification. It supports building via CMake and can be easily integrated into existing projects. With a stable API, it is widely used for configuration files and data serialization in C++ applications.

README Preview

# yaml-cpp  [](https://codedocs.xyz/jbeder/yaml-cpp/)\n\n`yaml-cpp` is a [YAML](http://www.yaml.org/) parser and emitter in C++ matching the [YAML 1.2 spec](http://www.yaml.org/spec/1.2/spec.html).\n\n## Usage\n\nSee [Tutorial](https://github.com/jbeder/yaml-cpp/wiki/Tutorial) and [How to Emit YAML](https://github.com/jbeder/yaml-cpp/wiki/How-To-Emit-YAML) for reference. For the old API (until 0.5.0), see [How To Parse A Document](https://github.com/jbeder/yaml-cpp/wiki/How-To-Parse-A-Document-(Old-API)).\n\n## How to Build\n\n`yaml-cpp` uses [CMake](http://www.cmake.org) to support cross-platform building. Install [CMake](http://www.cmake.org) _(Resources -> Download)_ before proceeding. The basic steps to build are:\n\n**Note:** If you don't use the provided installer for your platform, make sure that you add `CMake`'s bin folder to your path.\n\n#### 1. Navigate into the source directory, create build folder and run `CMake`:\n\n```sh\nmkdir build\ncd build\ncmake [-G generator] [-DYAML_BUILD_SHARED_LIBS=on|OFF] ..\n```\n\n  * The `generator` option is the build system you'd like to use. Run `cmake` without arguments to see a full list of available generators.\n    * On Windows, you might use "Visual Studio 12 2013" (VS 2013 32-bits), or "Visual Studio 14 2015 Win64" (VS 2015 64-bits).\n    * On OS X, you might use "Xcode".\n    * On a UNIX-like system, omit the option (for a Makefile).\n\n  * `yaml-cpp` builds a static library by default, you may want to build a shared library by specifying `-DYAML_BUILD_SHARED_LIBS=ON`.\n\n  * [Debug mode of the GNU standard C++\n    library](https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode.html)\n    can be used when both `yaml-cpp` and client code is compiled with the\n    `_GLIBCXX_DEBUG` flag (e.g. by calling CMake with `-D\n    CMAKE_CXX_FLAGS_DEBUG='-g -D_GLIBCXX_DEBUG'` option).\n\n    Note that for `yaml-cpp` unit tests to run successfully, the _GoogleTest_\n    library also must be built with this flag, i.e. t

FAQ (5)

Troubleshooting
How do I fix the yaml-cpp link error "relocation R_X86_64_PC32 ... can not be used when making a shared object; recompile with -fPIC"?

This means the installed libyaml-cpp.a was built without position-independent code, but your project is linking it into a shared library. Rebuild and reinstall yaml-cpp with PIC enabled:

git clone https://github.com/jbeder/yaml-cpp.git
cd yaml-cpp
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON ..
cmake --build .
sudo make install

Alternatively, pass the flags directly:

env CFLAGS="-fPIC" CXXFLAGS="-fPIC" cmake -DCMAKE_BUILD_TYPE=Release ..
make
sudo make install

Another workaround is to build yaml-cpp as a shared library instead:

cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON ..
make
sudo make install

Use a clean build directory or delete CMakeCache.txt first so the new compiler flags are applied.

GitHub Issue #682
Troubleshooting
How do I fix the Clang UBSan "reference binding to null pointer" error when calling YAML::Binary::data() on an empty Binary?

Upgrade yaml-cpp to a release containing the fix for issue #1248, or apply the linked PR. The undefined behavior comes from &m_data[0] being evaluated when the internal std::vector is empty. Replace it with std::vector::data(), which is valid for an empty vector:

char* data() { return m_data.data(); }
const char* data() const { return m_data.data(); }

As a temporary local workaround, you can also guard the old code:

return m_data.empty() ? nullptr : &m_data[0];

Rebuild yaml-cpp and your project after the change.

GitHub Issue #1248
Troubleshooting
How do I build yaml-cpp with the static MSVC runtime (/MT) so it links with a statically-linked MFC project?

Build yaml-cpp as a static library with the shared MSVC runtime disabled:

cmake -S . -B build -DYAML_BUILD_SHARED_LIBS=OFF -DYAML_MSVC_SHARED_RT=OFF
cmake --build build --config Release

For Debug builds, use --config Debug; CMake will then select /MTd.

In older yaml-cpp versions where YAML_MSVC_SHARED_RT defaults to ON, set it explicitly to OFF. Alternatively, in each generated Visual Studio project, set Project Properties → C/C++ → Code Generation → Runtime Library to Multi-Threaded (/MT) for Release or Multi-Threaded Debug (/MTd) for Debug.

When consuming the static library, define YAML_CPP_STATIC_DEFINE for your target:

target_compile_definitions(myapp PRIVATE YAML_CPP_STATIC_DEFINE)
target_link_libraries(myapp PRIVATE yaml-cpp)

Or add YAML_CPP_STATIC_DEFINE to your Visual Studio project's preprocessor definitions. Ensure yaml-cpp and your MFC application use the same runtime library setting to avoid linker conflicts.

GitHub Issue #1354
Troubleshooting
How to make yaml-cpp YAML::Load throw when the input std::istream has failbit or badbit set?

Upgrade to a yaml-cpp version containing the fix for issue #408. YAML::Load and YAML::LoadAll now reject streams that are already in a failed state and propagate stream failures encountered during parsing, while empty or EOF-only streams still produce a null node or no documents. On older versions, enable stream exceptions before parsing:

stream.exceptions(std::ios::failbit | std::ios::badbit);
try {
    YAML::Node node = YAML::Load(stream);
} catch (const std::ios::failure& e) {
    // Handle failed or unreadable input stream.
}

If the stream may already be failed, also check it first:

if (!stream) {
    throw std::runtime_error("input stream is already in a failed state");
}

GitHub Issue #408
Troubleshooting
How to fix undefined symbols when statically linking yaml-cpp on Windows with pkg-config?

The static pkg-config metadata must define YAML_CPP_STATIC_DEFINE so yaml-cpp headers use static symbol declarations. Upgrade to a yaml-cpp release containing the fix, or manually add this line to the installed yaml-cpp.pc file:

Cflags.private: -DYAML_CPP_STATIC_DEFINE

Then use pkg-config with --static, for example:

pkg-config --static --cflags yaml-cpp
pkg-config --static --libs yaml-cpp

As an immediate workaround, compile your project with -DYAML_CPP_STATIC_DEFINE. The define should only be used for static builds; shared-build pkg-config output must not include it.

GitHub Issue #1345