OpenSource-Hub

yaml-cpp

라이브러리

jbeder/yaml-cpp

YAML 1.2를 파싱하고 생성하기 위한 C++ 라이브러리입니다.

개요

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

FAQ (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를 삭제하세요.

원본 Issue #682
문제 해결
빈 Binary에서 YAML::Binary::data()를 호출할 때 발생하는 Clang UBSan "reference binding to null pointer" 오류는 어떻게 수정하나요?

issue #1248에 대한 수정이 포함된 릴리스로 yaml-cpp를 업그레이드하거나, 링크된 PR을 적용하세요. 정의되지 않은 동작은 내부 std::vector가 비어 있을 때 &m_data[0]이 평가되면서 발생합니다. 이를 빈 벡터에서도 유효한 std::vector::data()로 교체하세요:

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

임시 로컬 해결 방법으로, 기존 코드를 다음과 같이 가드할 수도 있습니다:

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

변경 후 yaml-cpp와 프로젝트를 다시 빌드하세요.

원본 Issue #1248
문제 해결
yaml-cpp를 정적 MSVC 런타임(/MT)으로 빌드하여 정적으로 링크된 MFC 프로젝트와 링크하려면 어떻게 해야 합니까?

공유 MSVC 런타임을 비활성화한 상태로 yaml-cpp를 정적 라이브러리로 빌드하세요:

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 프로젝트에서 Project Properties → C/C++ → Code Generation → Runtime Library를 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)

또는 Visual Studio 프로젝트의 전처리기 정의에 YAML_CPP_STATIC_DEFINE을 추가하세요. 링커 충돌을 방지하려면 yaml-cpp와 MFC 애플리케이션이 동일한 런타임 라이브러리 설정을 사용하는지 확인하세요.

원본 Issue #1354
문제 해결
입력 std::istream에 failbit 또는 badbit가 설정되어 있을 때 yaml-cpp YAML::Load가 예외를 throw하도록 만들려면 어떻게 해야 하나요?

issue #408에 대한 수정이 포함된 yaml-cpp 버전으로 업그레이드하세요. YAML::Load와 YAML::LoadAll은 이제 이미 실패 상태인 스트림을 거부하고 파싱 중에 발생한 스트림 실패를 전파하며, 비어 있거나 EOF만 있는 스트림은 여전히 null 노드를 생성하거나 문서를 생성하지 않습니다. 이전 버전에서는 파싱 전에 스트림 예외를 활성화하세요:

stream.exceptions(std::ios::failbit | std::ios::badbit);
try {
    YAML::Node node = YAML::Load(stream);
} catch (const std::ios::failure& e) {
    // 실패했거나 읽을 수 없는 입력 스트림을 처리합니다.
}

스트림이 이미 실패 상태일 수 있는 경우, 먼저 확인하세요:

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

원본 Issue #408
문제 해결
Windows에서 pkg-config를 사용해 yaml-cpp를 정적으로 링크할 때 발생하는 정의되지 않은 심볼 문제를 해결하는 방법은?

정적 pkg-config 메타데이터는 yaml-cpp 헤더가 정적 심볼 선언을 사용하도록 YAML_CPP_STATIC_DEFINE을 정의해야 합니다. 해당 수정 사항이 포함된 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을 사용하여 프로젝트를 컴파일하세요. 이 define은 정적 빌드에만 사용해야 하며, 공유 빌드의 pkg-config 출력에는 포함되어서는 안 됩니다.

원본 Issue #1345