
编译库位置
ros环境的yaml会干扰正常环境,CMakeLists要修改下手动指定build文件夹下编译的库


CMakeLists.txt
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})
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
#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
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
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
#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;
}
浙公网安备 33010602011771号