欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Ubuntu安装ymal

时间:2023-04-26
Ubuntu安装ymal 1 编译yaml-cpp库

yaml-cpp是github上的一个开源库,通过CMake编译

下载好了,里面有帮助文档,根据帮助文档编译即可

mkdir buildcd buildcmake ..makemake install

2 demo测试

编写一个demo文件,创建一个config.yaml配置文件

name: RandyTel: 18666666 age: 18languages: - Ruby - Perl - Pythonskills: c++: 1 java: 1 android: 1 python: 1label: QQ: hello cc: world JJ: QCJcontainers: - name: hh age: 18 - name: kk age: 12 - name: yy age: 30

main.cpp内容:

#include #include #include "ymal-cpp/yaml.h"#include class Containers{public: std::string m_name; int m_age; static std::vector YamlToContainers(YAML::Node& node) { std::vector containers; for (auto it = node["containers"].begin(); it != node["containers"].end(); it++) { Containers container; container.m_name = it->operator[]("name").as(); container.m_age = it->operator[]("age").as(); containers.push_back(container); } return containers; } std::string ToString(Containers container) { std::stringstream ss; ss << "name: " << container.m_name << 'n' << "age: " << container.m_age << 'n'; return ss.str(); }};int main(int argc, char** argv){ YAML::Node config = YAML::LoadFile("/home/qiancj/Vmware16/ShareFolder/5Codes/yaml_demo/config.yaml"); std::cout << "name:" << config["name"].as() << std::endl; std::cout << "Tel:" << config["Tel"].as() << std::endl; std::cout << "age:" << config["age"].as() << std::endl; std::cout << "skills:" << config["skills"]["c++"].as() << std::endl; // 读取languages for (auto it = config["languages"].begin(); it != config["languages"].end(); it++) { std::cout << it->as() << std::endl; } // 读取label for (auto it = config["label"].begin(); it != config["label"].end(); it++) { std::cout << it->first.as() << ": " << it->second.as() << std::endl; } // 读取containers std::vector p_set = Containers::YamlToContainers(config); for(auto it : p_set) { std::cout << it.ToString(it) << std::endl; } // 写入yaml YAML::Node node; node["Countries"].push_back("China"); node["Countries"].push_back("Russia"); node["Countries"].push_back("Africa"); node["Colors"]["Black"] = "night"; node["Colors"]["Red"] = "sun"; node["Colors"]["Yellow"] = "flower"; node["Colors"]["Green"] = "tree"; std:: cout << node << std::endl; std::ofstream ofs("config.yaml"); ofs << node; ofs.close(); return 0;}

CMakeLists.txt配置如下

project(yaml_demo)add_definitions(-std=c++11)include_directories(include)set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) # 二进制文件的输出目录Message(STATUS, "PRINT ${PROJECT_SOURCE_DIR}")link_directories(${PROJECT_SOURCE_DIR}/lib/yaml-cpp)add_executable(${PROJECT_NAME} main.cpp)target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/lib/yaml-cpp/libyaml-cpp.a)

编译过程中总是报错

/tmp/ccCvErNZ.o: In function `YAML::detail::node& YAML::detail::node_data::get(std::string const&, std::shared_ptr)':cricket.cpp:(.text._ZN4YAML6detail9node_data3getISsEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE[_ZN4YAML6detail9node_data3getISsEERNS0_4nodeERKT_St10shared_ptrINS0_13memory_holderEE]+0x94): undefined reference to `YAML::detail::node_data::convert_to_map(std::shared_ptr)'collect2: error: ld returned 1 exit status

stack overflow上建议直接安装libyaml:

apt-get --build source libyaml-cpp0.5/stable//ordpkg -i libyaml-cpp0.5*.deb

还是不行,需要在CMakeLists.txt中添加:

FIND_PACKAGE(yaml-cpp REQUIRED)

再次测试可以运行

且生成了新的yaml文件

参考

c++如何使用yaml来进行配置

c++ 解析yaml文件

c++ 开发中利用yaml-cpp读写yaml配置文件

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。