项目简介
yaml-cpp 是一个 C++ 库,提供符合 YAML 1.2 规范的解析与生成功能。支持通过 CMake 构建,可轻松集成到项目中。API 稳定,广泛用于 C++ 应用的配置文件读写与数据序列化。
README 预览
# 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
常见问题 (5)
故障排除如何修复 yaml-cpp 链接错误 "relocation R_X86_64_PC32 ... can not be used when making a shared object; recompile with -fPIC"?
这意味着已安装的 libyaml-cpp.a 在构建时未使用位置无关代码(position-independent code),而你的项目正在将它链接到一个共享库中。请在启用 PIC 的情况下重新构建并安装 yaml-cpp:
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
或者,直接传递这些标志:
env CFLAGS="-fPIC" CXXFLAGS="-fPIC" cmake -DCMAKE_BUILD_TYPE=Release ..
make
sudo make install
另一种解决方法是改为将 yaml-cpp 构建为共享库:
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON ..
make
sudo make install
请使用干净的构建目录,或先删除 CMakeCache.txt,以便新的编译器标志能够生效。
故障排除如何修复在空的 Binary 上调用 YAML::Binary::data() 时出现的 Clang UBSan "reference binding to null pointer" 错误?
将 yaml-cpp 升级到包含 issue #1248 修复的发布版本,或者应用所链接的 PR。该未定义行为源于当内部 std::vector 为空时对 &m_data[0] 进行求值。请将其替换为 std::vector::data(),它对于空 vector 也是有效的:
char* data() { return m_data.data(); }
const char* data() const { return m_data.data(); }
作为临时的本地变通方案,你也可以为旧代码添加保护判断:
return m_data.empty() ? nullptr : &m_data[0];
修改完成后,重新构建 yaml-cpp 和你的项目。
故障排除如何使用静态 MSVC 运行时 (/MT) 构建 yaml-cpp,使其能够与静态链接的 MFC 项目链接?
将 yaml-cpp 构建为静态库,并禁用共享 MSVC 运行时:
cmake -S . -B build -DYAML_BUILD_SHARED_LIBS=OFF -DYAML_MSVC_SHARED_RT=OFF
cmake --build build --config Release
对于 Debug 构建,请使用 --config Debug;此时 CMake 将选择 /MTd。
在 YAML_MSVC_SHARED_RT 默认为 ON 的旧版本 yaml-cpp 中,请将其显式设置为 OFF。或者,在每个生成的 Visual Studio 项目中,将"项目属性 → C/C++ → 代码生成 → 运行时库"设置为:Release 使用 Multi-Threaded (/MT),Debug 使用 Multi-Threaded Debug (/MTd)。
在使用该静态库时,请为您的目标定义 YAML_CPP_STATIC_DEFINE:
target_compile_definitions(myapp PRIVATE YAML_CPP_STATIC_DEFINE)
target_link_libraries(myapp PRIVATE yaml-cpp)
或者,将 YAML_CPP_STATIC_DEFINE 添加到您的 Visual Studio 项目的预处理器定义中。请确保 yaml-cpp 与您的 MFC 应用程序使用相同的运行时库设置,以避免链接器冲突。
故障排除如何让 yaml-cpp 的 YAML::Load 在输入的 std::istream 设置了 failbit 或 badbit 时抛出异常?
升级到包含 issue #408 修复的 yaml-cpp 版本。YAML::Load 和 YAML::LoadAll 现在会拒绝已处于失败状态的流,并传播解析过程中遇到的流失败,而空流或仅包含 EOF 的流仍会产生空节点或不产生任何文档。在旧版本上,请在解析前启用流异常:
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 (!stream) {
throw std::runtime_error("input stream is already in a failed state");
}故障排除如何在 Windows 上使用 pkg-config 静态链接 yaml-cpp 时修复未定义符号错误?
静态 pkg-config 元数据必须定义 YAML_CPP_STATIC_DEFINE,以便 yaml-cpp 头文件使用静态符号声明。请升级到包含此修复的 yaml-cpp 版本,或手动将以下行添加到已安装的 yaml-cpp.pc 文件中:
Cflags.private: -DYAML_CPP_STATIC_DEFINE
然后使用带 --static 选项的 pkg-config,例如:
pkg-config --static --cflags yaml-cpp
pkg-config --static --libs yaml-cpp
作为临时解决方案,可以在编译项目时使用 -DYAML_CPP_STATIC_DEFINE。该宏仅应用于静态构建;共享构建的 pkg-config 输出不得包含它。