编译库位置
ros环境的yaml会干扰正常环境,CMakeLists要修改下手动指定build文件夹下编译的库
CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | cmake_minimum_required(VERSION 3.5) project(YamlCppExample) # 设置C++标准 set (CMAKE_CXX_STANDARD 11) # 查找yaml-cpp包 - ros 环境被干扰使用错误,报错段错误或者找不到 #find_package(yaml-cpp REQUIRED) # 添加可执行文件 add_executable(node main.cpp) # 链接yaml-cpp库 - ros 环境被干扰使用错误,报错段错误或者找不到 #target_link_libraries(node yaml-cpp) # 如果yaml-cpp没有自动发现,防止被ros环境干扰,手动找到编译的库位置: set (YAML_CPP_LIBRARIES "/home/dongdong/1sorftware/1work/openvslam/yaml-cpp-0.6.3/BUILD/libyaml-cpp.a" ) target_link_libraries(node ${YAML_CPP_LIBRARIES}) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | cmake_minimum_required(VERSION 3.10) project(EigenYamlExample) # Set C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) # Find the Eigen library find_package(Eigen3 REQUIRED) # Find the yaml-cpp library find_package(yaml-cpp REQUIRED) # Include directories include_directories(${EIGEN3_INCLUDE_DIR} ${YAML_CPP_INCLUDE_DIR}) # Add executable add_executable(main main.cpp) # Link libraries target_link_libraries(main Eigen3::Eigen yaml-cpp) |
main.cpp
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include <iostream> #include <yaml-cpp/yaml.h> #include <fstream> using namespace std; int save_yaml(std:: string sava_path) { // 创建一个YAML文档 YAML::Emitter emitter; // 开始写入YAML文档 emitter << YAML::BeginMap; emitter << YAML::Key << "name" ; emitter << YAML::Value << "Alice" ; emitter << YAML::Key << "age" ; emitter << YAML::Value << 25; emitter << YAML::Key << "city" ; emitter << YAML::Value << "London" ; // 结束写入YAML文档 emitter << YAML::EndMap; // 将YAML数据写入文件 std::ofstream fout(sava_path); fout << emitter.c_str(); fout.close(); std::cout << "YAML data saved to output.yaml" << std::endl; return 0; } int read_yaml(std:: string read_path) { try { // 读取YAML文件 YAML::Node config = YAML::LoadFile(read_path); // 访问YAML中的数据 std:: string name = config[ "name" ]. as <std:: string >(); int age = config[ "age" ]. as < int >(); std:: string city = config[ "city" ]. as <std:: string >(); // 打印读取的数据 std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "City: " << city << std::endl; } catch ( const YAML::Exception& e) { std::cerr << "YAML Exception: " << e.what() << std::endl; return 1; } return 0; } int main() { string config_path = "../config.yaml" ; read_yaml(config_path); string sava_path = "../out.yaml" ; save_yaml(sava_path) ; } |
样例2
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 32 33 34 35 36 37 38 | Matrix4d: - - 1 - 0 - 0 - 0 - - 0 - 1 - 0 - 0 - - 0 - 0 - 1 - 0 - - 0 - 0 - 0 - 1 Matrix3d: - - 1 - 0 - 0 - - 0 - 1 - 0 - - 0 - 0 - 1 Vector3d: - 1 - 2 - 3 |
CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | cmake_minimum_required(VERSION 3.10) project(EigenYamlExample) # Set C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) # Find the Eigen library find_package(Eigen3 REQUIRED) # Find the yaml-cpp library find_package(yaml-cpp REQUIRED) # Include directories include_directories(${EIGEN3_INCLUDE_DIR} ${YAML_CPP_INCLUDE_DIR}) # Add executable add_executable(main main.cpp) # Link libraries target_link_libraries(main Eigen3::Eigen yaml-cpp) |
main.cpp
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <iostream> #include <fstream> #include <yaml-cpp/yaml.h> #include <Eigen/Dense> // Serialize Eigen::Matrix4d to YAML YAML::Emitter& operator<<(YAML::Emitter& out, const Eigen::Matrix4d& mat) { out << YAML::BeginSeq; for ( int i = 0; i < 4; ++i) { out << YAML::BeginSeq; for ( int j = 0; j < 4; ++j) { out << mat(i, j); } out << YAML::EndSeq; } out << YAML::EndSeq; return out; } // Deserialize Eigen::Matrix4d from YAML void operator>>( const YAML::Node& node, Eigen::Matrix4d& mat) { for ( int i = 0; i < 4; ++i) { for ( int j = 0; j < 4; ++j) { mat(i, j) = node[i][j].as< double >(); } } } // Serialize Eigen::Matrix3d to YAML YAML::Emitter& operator<<(YAML::Emitter& out, const Eigen::Matrix3d& mat) { out << YAML::BeginSeq; for ( int i = 0; i < 3; ++i) { out << YAML::BeginSeq; for ( int j = 0; j < 3; ++j) { out << mat(i, j); } out << YAML::EndSeq; } out << YAML::EndSeq; return out; } // Deserialize Eigen::Matrix3d from YAML void operator>>( const YAML::Node& node, Eigen::Matrix3d& mat) { for ( int i = 0; i < 3; ++i) { for ( int j = 0; j < 3; ++j) { mat(i, j) = node[i][j].as< double >(); } } } // Serialize Eigen::Vector3d to YAML YAML::Emitter& operator<<(YAML::Emitter& out, const Eigen::Vector3d& vec) { out << YAML::BeginSeq; for ( int i = 0; i < 3; ++i) { out << vec[i]; } out << YAML::EndSeq; return out; } // Deserialize Eigen::Vector3d from YAML void operator>>( const YAML::Node& node, Eigen::Vector3d& vec) { for ( int i = 0; i < 3; ++i) { vec[i] = node[i].as< double >(); } } int main() { Eigen::Matrix4d mat4 = Eigen::Matrix4d::Identity(); Eigen::Matrix3d mat3 = Eigen::Matrix3d::Identity(); Eigen::Vector3d vec3(1.0, 2.0, 3.0); // Serialize data to YAML file YAML::Emitter out; out << YAML::BeginMap; out << YAML::Key << "Matrix4d" << YAML::Value << mat4; out << YAML::Key << "Matrix3d" << YAML::Value << mat3; out << YAML::Key << "Vector3d" << YAML::Value << vec3; out << YAML::EndMap; std::ofstream fout( "data.yaml" ); fout << out.c_str(); fout.close(); // Deserialize data from YAML file YAML::Node node = YAML::LoadFile( "data.yaml" ); Eigen::Matrix4d loadedMat4; Eigen::Matrix3d loadedMat3; Eigen::Vector3d loadedVec3; node[ "Matrix4d" ] >> loadedMat4; node[ "Matrix3d" ] >> loadedMat3; node[ "Vector3d" ] >> loadedVec3; // Print the loaded data std::cout << "Loaded Matrix4d:\n" << loadedMat4 << std::endl; std::cout << "Loaded Matrix3d:\n" << loadedMat3 << std::endl; std::cout << "Loaded Vector3d:\n" << loadedVec3 << std::endl; return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-08-01 经典论文 imu数据融合算法
2018-08-01 天猫精灵接入