[Basic]YAML파일 사용하기/Using YAML files[MacOS]

👉🏻 yaml파일을 c++에서 사용하기 위해서는 라이브러리를 사용합니다.
To use YAML files in C++, use a library.

👉🏻 설치 방법은 다음과 같습니다.
The installation method is as follows.

✔️Home Brew

— 설치가 완료되면 /usr/local/opt/yaml-cpp/your version 또는 /opt/homebrew/opt/yaml-cpp 경로에 라이브러리가 설치됩니다. 이후 C++ 프로젝트에서 해당 경로를 포함하여 사용하시면 됩니다.
Once the installation is complete, the library will be installed in the /usr/local/opt/yaml-cpp/your version or /opt/homebrew/opt/yaml-cpp path. Afterwards, you can use it by including that path in your C++ projects.

$ brew install yaml-cpp
$ brew install yaml-cpp
... 중간 생략 / skipping the middle ...
You have 51 outdated formulae and 2 outdated casks installed.

==> Fetching downloads for: yaml-cpp
✔︎ Bottle Manifest yaml-cpp (0.9.0)                   Downloaded    7.3KB/  7.3KB
✔︎ Bottle yaml-cpp (0.9.0)                            Downloaded  131.2KB/131.2KB
==> Pouring yaml-cpp--0.9.0.arm64_tahoe.bottle.tar.gz
🍺  /opt/homebrew/Cellar/yaml-cpp/0.9.0: 51 files, 478.4KB
==> Running `brew cleanup yaml-cpp`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).

✔️ github download

https://github.com/jbeder/yaml-cpp

✔️ install

git clone https://github.com/jbeder/yaml-cpp.git
cd yaml-cpp
mkdir build && cd build
cmake ..
make
sudo make install

👉🏻 yaml-cpp 라이브러리 설치 후 / After installing the yaml-cpp library

👉🏻코드 / Code

✔️ 0. 디렉토리 구조 / Directory Structure

MacBookAir yaml % ls
cmake		config.yaml	default

MacBookAir cmake % ls
build		CMakeLists.txt	main.cpp

MacBookAir default % ls
main.cpp

✔️ 1. yaml-cpp 라이브러리 설치 및 포함 / Installing and including the yaml-cpp library

#include <yaml-cpp/yaml.h>

✔️ 2. YAML 파일 예시 / YAML file example

server:
  host: "localhost"
  port: 8080

database:
  user: "root"
  password: "1234"
  dbname: "testdb"

✔️ 3. YAML 파일 읽기 예제 코드(main.cpp)
YAML file reading example code (main.cpp)

— cmake를 사용하면 ../config.yaml 은 ../../config.yaml이 됩니다.
If you use cmake, ../config.yaml becomes ../../config.yaml.

#include <iostream>
#include <yaml-cpp/yaml.h>

int main() {
    try {
        YAML::Node config = YAML::LoadFile("../config.yaml");

        std::string host = config["server"]["host"].as<std::string>();
        int port = config["server"]["port"].as<int>();

        std::string db_user = config["database"]["user"].as<std::string>();
        std::string db_password = config["database"]["password"].as<std::string>();
        std::string db_name = config["database"]["dbname"].as<std::string>();

        std::cout << "Server Host: " << host << std::endl;
        std::cout << "Server Port: " << port << std::endl;
        std::cout << "Database User: " << db_user << std::endl;
        std::cout << "Database Name: " << db_name << std::endl;

    } catch (const std::exception &e) {
        std::cerr << "Error loading config.yaml: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

✔️ 컴파일 / Compiling – default

g++ -std=c++17 -I/opt/homebrew/Cellar/yaml-cpp/0.9.0/include -L/opt/homebrew/Cellar/yaml-cpp/0.9.0/lib -lyaml-cpp main.cpp -o main

✔️빌드 / build – cmake

# 프로젝트 디렉토리내에서 실행
# Run within the project directory

cd cmake
mkdir build && cd build
cmake ..
make

✔️ 실행 / Run

— cmake

$ cd project directory/cmake/build
$ ./main

— default

$ cd project directory/cmake/build
$ ./main

✔️ 결과 / Result

 % ./main
Server Host: localhost
Server Port: 8080
Database User: root
Database Name: testdb

Leave a Reply