【protobuf】protobuf自定义数据格式,CMake编译C++文件读写自定义数据

protobuf自定义数据格式,CMake编译文件读写自定义数据

1.protobuf安装

protobuf库链接

2.定义.proto文件

新建一个Person.proto文件和一个Animal.proto文件,内容如下:

cpp 复制代码
syntax = "proto3";

package UserInfo;

message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;
}
cpp 复制代码
syntax = "proto3";

package UserInfo;

message Animal {
  string name = 1;
  int32 id = 2;
  int32 age = 3;
}

3.编写main.cpp

main函数中对proto中的对象进行序列化和反序列化,内容如下:

cpp 复制代码
#include "Animal.pb.h"
#include "Person.pb.h"
#include <fstream>
#include <iostream>

int main() {
  {
    // 创建 Person 对象
    UserInfo::Person person;
    person.set_name("John Doe");
    person.set_id(123);
    person.set_email("john.doe@example.com");

    // 将数据序列化到文件
    std::ofstream output("person_data", std::ios::binary);
    person.SerializeToOstream(&output);
    output.close();

    // 从文件中读取数据并反序列化
    UserInfo::Person read_person;
    std::ifstream input("person_data", std::ios::binary);
    read_person.ParseFromIstream(&input);
    input.close();

    // 打印反序列化后的数据
    std::cout << "Name: " << read_person.name() << std::endl;
    std::cout << "ID: " << read_person.id() << std::endl;
    std::cout << "Email: " << read_person.email() << std::endl;
  }
  { // 创建 Animal 对象
    UserInfo::Animal animal;
    animal.set_name("Dog");
    animal.set_id(456);
    animal.set_age(5);

    // 将数据序列化到文件
    std::ofstream output("animal_data", std::ios::binary);
    animal.SerializeToOstream(&output);
    output.close();

    // 从文件中读取数据并反序列化
    UserInfo::Animal read_animal;
    std::ifstream input("animal_data", std::ios::binary);
    read_animal.ParseFromIstream(&input);
    input.close();

    // 打印反序列化后的数据
    std::cout << "Name: " << read_animal.name() << std::endl;
    std::cout << "ID: " << read_animal.id() << std::endl;
    std::cout << "age: " << read_animal.age() << std::endl;
  }

  return 0;
}

&ems;注意这时候我们的main.cpp文件是报错的,因为找不到Animal.h文件和Person.h文件,没关系,接下来编辑CMAkeLists.txt配置文件,生成相应的头文件即可!

4.编写CMAkeLists配置文件

cpp 复制代码
cmake_minimum_required(VERSION 3.5)
project(YourProjectName)

set(CMAKE_CXX_STANDARD 11)

# 查找protobuf库
find_package(Protobuf REQUIRED)

# 获取所有的protobuf文件
file(GLOB PROTO_FILES "proto/*.proto")

# 生成protobuf文件的C++代码
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})

# 添加生成的protobuf文件到项目中
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${PROTOBUF_INCLUDE_DIRS})

# 添加可执行文件
add_executable(main main.cpp ${PROTO_SRCS} ${PROTO_HDRS})

# 链接protobuf库
target_link_libraries(main ${PROTOBUF_LIBRARIES})

5.运行

cpp 复制代码
mkdir build
cd build
cmake ..
make 
./main

结果如下:

相关推荐
程序猿编码9 分钟前
Linux 文件变动监控工具:原理、设计与实用指南(C/C++代码实现)
linux·c语言·c++·深度学习·inotify
好好学习啊天天向上1 小时前
多维c++ vector, vector<pair<int,int>>, vector<vector<pair<int,int>>>示例
开发语言·c++·算法
我狸才不是赔钱货1 小时前
CUDA:通往大规模并行计算的桥梁
c++·人工智能·pytorch
winds~2 小时前
【GUI】本地电脑弹出远程服务器的软件GUI界面
运维·服务器·c++
杨筱毅4 小时前
【穿越Effective C++】条款8:别让异常逃离析构函数——C++异常安全的关键支柱
c++·effective c++
code monkey.4 小时前
【探寻C++之旅】C++ 智能指针完全指南:从原理到实战,彻底告别内存泄漏
c++·c++11·智能指针
草莓熊Lotso5 小时前
《算法闯关指南:优选算法--前缀和》--27.寻找数组的中心下标,28.除自身以外数组的乘积
开发语言·c++·算法·rpc
Cx330❀6 小时前
《C++ 继承》三大面向对象编程——继承:派生类构造、多继承、菱形虚拟继承概要
开发语言·c++
cookies_s_s6 小时前
项目--缓存系统(C++)
c++·缓存