yaml做爲一種便捷的文件格式,經過C++進行操做時,能夠利用yaml-cpp進行。html
git clone https://github.com/jbeder/yaml-cpp.git
mkdir build cd build cmake -DBUILD_SHARED_LIBS=ON .. # ON 設置生成共享庫 sudo make install
pkg-config --modversion yaml-cpp
YAML::Node node1 = YAML::LoadFile("config.yaml"); // 加載文件 YAML::Node node2 = YAML::Load("[1,2,3]"); // 加載數組 cout << node1[0].as<string>() << endl; cout << node2[0].as<int>() << endl; // 輸出元素
更多API參考yaml-cpp docs。node
cmake_minimum_required(VERSION 2.8) project(yaml_test) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") link_directories(/home/jonty/Softwares/yaml-cpp/build) include_directories(/home/jonty/Softwares/yaml-cpp/include/yaml-cpp) add_executable(test main.cpp) target_link_libraries(test yaml-cpp)
#include <iostream> #include "yaml.h" using namespace std; int main() { YAML::Node node = YAML::Load("[22,3,4,4]"); cout << node[0] << endl; YAML::Node node2 = YAML::LoadFile("../config.yaml"); cout << node2["ttt"] << endl; return 0; }
mkdir build cd build cmake .. make ./test
參考教程
tutorialsios