0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

yaml-cpp導入記録

Last updated at Posted at 2024-12-29

概要

yaml-cppを導入するにあたり、環境構築で戸惑ったので記録した。

目的

  1. C++でYAMLファイルを読み込む
  2. CMakeを用いたbuildの練習
  3. 外部ライブラリを扱う練習

環境

  • M1 MacBook Air Sonoma 14.7.1
  • Homebrew 4.4.13
  • GCC 14.2.0_1
  • CMake 3.31.3
  • yaml-cpp stable 0.8.0

手順

Homebrewでyaml-cppをinstall

$ brew install yaml-cpp

installされたか確認する。

$ brew list | grewp yaml-cpp

CMakeLists.txtを作成

テスト用でシンプルなファイル構成にする。下のリンク先のコードを拝借した。

yaml_cpp_test
├── CMakeLists.txt
└── main.cpp

main.cpp を作成。

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

int main()
{
    YAML::Node node = YAML::Load("key: value");

    // valueが出力される
    std::cout << "key == " << node["key"] << std::endl;

    // これは何も出力されない
    std::cout << "key2 == " << node["key2"] << std::endl;

    // これだと例外発生
    try
    {
        std::cout << "key2 == " << node["key2"].as<std::string>() << std::endl;
    }
    catch (const YAML::TypedBadConversion<std::string> &e)
    {
        std::cout << "エラー: " << e.msg << std::endl;
    }

    // aaaが出力される
    std::cout << "key2 == " << node["key2"].as<std::string>("aaa") << std::endl;
    return 0;
}

CMakeLists.txtを作成。

# CMakeのバージョンを設定
cmake_minimum_required(VERSION 3.13)

# C++のバージョンを指定
set(CMAKE_CXX_STANDARD 11)

# プロジェクト名と使用する言語を設定
project(yaml_test)

# ライブラリのpathを追加
link_directories("/opt/homebrew/lib")
include_directories("/opt/homebrew/include")

find_package(yaml-cpp)

# a.outという実行ファイルをmain.cppとhello.cppから作成
add_executable(a.out main.cpp)

# a.outを作成する際にyaml-cppをリンク
target_link_libraries(a.out yaml-cpp)

build

cmake -S . -B build
cmake --build build

実行

$ ./build/a.out 
key == value
key2 == 
key2 == エラー

エラー対応など

ビルド時に大量のエラー発生

/opt/homebrew/include/yaml-cpp/binary.h:23:17: error: expected member name or ';' after declaration specifiers
   23 |       : m_data{}, m_unownedData(data_), m_unownedSize(size_) {}
      |                 ^
/opt/homebrew/include/yaml-cpp/binary.h:23:15: error: expected '('
   23 |       : m_data{}, m_unownedData(data_), m_unownedSize(size_) {}
      |               ^
In file included from /Users/murakamiritsu/tests/yaml_cpp_test/main.cpp:2:
In file included from /opt/homebrew/include/yaml-cpp/yaml.h:14:
In file included from /opt/homebrew/include/yaml-cpp/exceptions.h:12:
/opt/homebrew/include/yaml-cpp/traits.h:111:10: error: 'auto' not allowed in function return type
  111 |   static auto test(int)
      |          ^~~~
/opt/homebrew/include/yaml-cpp/traits.h:111:24: error: expected ';' at end of declaration list
  111 |   static auto test(int)
      |                        ^
/opt/homebrew/include/yaml-cpp/traits.h:115:10: error: 'auto' not allowed in function return type
  115 |   static auto test(...) -> std::false_type;
      |          ^~~~
/opt/homebrew/include/yaml-cpp/traits.h:115:24: error: expected ';' at end of declaration list
  115 |   static auto test(...) -> std::false_type;
      |                        ^
...

...
/opt/homebrew/include/yaml-cpp/exceptions.h:243:19: error: expected ';' at end of declaration list
  243 |   ~BadConversion() YAML_CPP_NOEXCEPT override;
      |                   ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
gmake[2]: *** [CMakeFiles/a.out.dir/build.make:79: CMakeFiles/a.out.dir/main.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:87: CMakeFiles/a.out.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 

バージョンを指定する必要があるみたいです。詳しくは、参考欄のIssueへ。

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

参考

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?