c++ protobuf 练习
安装proto buffer
后续补上…
创建两个proto文件
- stu.proto (单个字段模式)
message stu
{
optional string name = 1;
optional int32 age = 2;
optional string addr = 3;
}
- stu_bat.prot (数组模式)
message student
{
optional string name = 1;
optional int32 age = 2;
optional string addr = 3;
}
message students
{
repeated student list = 1;
}
- 执行指令生成c++头文件,和cc文件
protoc.exe --cpp_out=. *.proto
生成四个文件:
stu.pb.cc
stu.pb.h
stu_bat.pb.cc
stu_bat.pb.h
编译测试
- 将生成的四个cpp代码拷贝到一个文件夹,进行测试
- 同目录下写测试代码如下:
#include<iostream>
#include"stu.pb.h"
#include"stu_bat.pb.h"
using namespace std;
void fun1(){
GOOGLE_PROT