Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 리뷰
- File
- bitcoin
- C
- mutex
- Sync
- window
- 책
- write
- range
- json
- c++
- http
- API
- channel
- install
- Python
- windows
- GO 언어
- JavaScript
- FOR
- Callback
- Golang
- go
- tcp
- package
- 영화
- Linux
- Close
- go언어
Archives
- Today
- Total
Code Habit
C++ 에서 YAML 다루기 : yaml-cpp 시작하기 본문
YAML 파일은 간결하고 직관적인 데이터 구조를 표현하는 형식으로 구성 파일에 주로 사용된다. yaml-cpp는 c++언어에서 yaml파일을 다룰 수 있는 강력한 라이브러리로, 다양한 yaml 기능을 지원하며 설치가 쉽고 사용이 간편하다. 이 글에서는 yaml-cpp의 기본적인 사용법을 단계별로 알아보겠다.
1. yaml-cpp 설치하기
1.1 git code 다운, 빌드 & 설치
git clone https://github.com/jbeder/yaml-cpp.git
cd yaml-cpp
mkdir build
cd build
cmake ..
make
sudo make install
1.2 CMakeLists.txt 파일에 yaml-cpp 라이브러리 추가
find_package(yaml-cpp REQUIRED)
target_link_libraries(your_project_name yaml-cpp)
2 yaml-cpp 사용 예제
2.1 yaml 파일예제
name: "Example"
version: 1.0
dependencies:
- yaml-cpp
- boost
- fmt
2.2 yaml 사용예제 ( cpp )
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <string>
int main() {
YAML::Node config = YAML::LoadFile("example.yaml");
// 단일 값 읽기
std::string name = config["name"].as<std::string>();
double version = config["version"].as<double>();
// 배열 값 읽기
for (auto dep : config["dependencies"]) {
std::cout << "Dependency: " << dep.as<std::string>() << std::endl;
}
std::cout << "Name: " << name << "\nVersion: " << version << std::endl;
return 0;
}
2.3 더 복잡한 구조 읽기
person:
name: "John Doe"
age: 30
address:
street: "1234 Main St"
city: "Hometown"
YAML::Node person = config["person"];
std::string name = person["name"].as<std::string>();
int age = person["age"].as<int>();
std::string street = person["address"]["street"].as<std::string>();
std::string city = person["address"]["city"].as<std::string>();