protobuf 基本使用

proto 文件格式

protobuf 复制代码
 syntax = "proto3";              // 指定版本信息,不指定会报错
 packag pb;                      // 后期生成go文件的包名
 // message为关键字,作用为定义一种消息类型
 message Person{
     string name = 1;   // 名字
     int32  age = 2 ;   // 年龄
 }
 ​
 enum test{
     int32 age = 0;
 }

protobuf 和 C++

  1. 没有设置默认值,也会有默认值,例如:布尔类型(bool)默认为 false。

  2. 定义类型,唯一的标识符,范围为1到2^29 - 1

  3. 导入头文件,首先是包名作为namespace,声明变量pb::Person person;

  4. proto2中required optional repeated 必须写,不写编译不通过

  5. proto3中只有repeated可用,写了其他的,编译不通过

  6. required:必须包含
    optional:可选包含
    repeated:可以重复包含

  7. 若required写了,在序列化时候,会报错

  8. 常见使用方法

    cpp 复制代码
    #include "person.pb.h"
    #include <iostream>
    using namespace std;
    
    
    int main() {
        // 创建一个 Person 对象并设置其字段
        pb::Person person;
        // 设置值,方式一
        person.set_name("chang");
        cout << person.name() << endl; 
    
        // 设置值,方式二
        person.mutable_name()->assign("jiang");
        cout << person.name() << endl; 
    
        // 设置值,方式三
        pb::Person person1;
        person.CopyFrom(person1);
        person.mutable_name()->assign("da");
        cout << person.name() << endl; 
    
        // 如果是消息体嵌套,方式一
        pb::Person::Student* student2 = person.add_s();
        student2->set_id("fef");
    
        student2 = person.add_s();
        student2->set_id("fef");
    
        cout << person.s_size() << endl;
        cout << person.s(0).id() << endl; 
        cout << person.s(1).id() << endl; 
    
        // 如果是消息体嵌套,方式二
        person.add_s();
        person.mutable_s(2)->set_id("e2");
        cout << person.s_size() << endl;
        cout << person.s(2).id() << endl; 
    
        // 序列化
        std::string serialized_data;
        person.SerializeToString(&serialized_data);
    
        // 反序列化
        pb::Person new_person;
        new_person.ParseFromString(serialized_data);
        return 0;
    }
  9. proto编译成c++,protoc --cpp_out=./ ./demo.proto

  10. c++编译成执行文件g++ test.cpp person.pb.cc -lprotobuf -pthread

protobuf 和 python

  1. 编译方式:protoc --python_out=./ person.proto

  2. 常见使用

    python 复制代码
    import person_pb2            # 直接导入编译好的文件
    p =  person_pb2.Person()     # 直接使用message类
    p.name = "fwef"
    print(p.name)
    
    # 将消息序列化为字节串
    serialized_data = p.SerializeToString()
    
    # 将字节串反序列化为消息对象
    new_person = person_pb2.Person()
    new_person.ParseFromString(serialized_data)

常见C++报错一:

bash 复制代码
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted (core dumped)

参考文档

解决方案:

bash 复制代码
# 增加参数 -pthread
g++ test.cpp person.pb.cc -lprotobuf -pthread
相关推荐
sukalot1 分钟前
windows C++-创建基于代理的应用程序(下)
c++
计算机源码社10 分钟前
分享一个餐饮连锁店点餐系统 餐馆食材采购系统Java、python、php三个版本(源码、调试、LW、开题、PPT)
java·python·php·毕业设计项目·计算机课程设计·计算机毕业设计源码·计算机毕业设计选题
labuladuo52013 分钟前
AtCoder Beginner Contest 372 F题(dp)
c++·算法·动态规划
汤兰月15 分钟前
Python中的观察者模式:从基础到实战
开发语言·python·观察者模式
DieSnowK16 分钟前
[C++][第三方库][httplib]详细讲解
服务器·开发语言·c++·http·第三方库·新手向·httplib
西柚与蓝莓2 小时前
【开源开放体系总结】
python
StrokeAce2 小时前
linux桌面软件(wps)内嵌到主窗口后的关闭问题
linux·c++·qt·wps·窗口内嵌
家有狸花5 小时前
VSCODE驯服日记(三):配置C++环境
c++·ide·vscode
belldeep5 小时前
python:reportlab 将多个图片合并成一个PDF文件
python·pdf·reportlab