kunpengdt
其他场景安装说明
更新时间:2020/04/18 GMT+08:00
在CentOS 7.6以yum方式安装Protobuf
由于CentOS 7.6的yum源中自带了Protobuf的RPM包,因此也可以采用yum直接安装。
安装方式如下:
yum install protobuf protobuf-devel -y
安装完成后,查看安装是否完成。
protoc --version
返回内容如下。
libprotoc 2.5.0
在EulerOS 2.8上以yum方式安装Protobuf
如果使用的OS是EulerOS 2.8,那么EulerOS yum源中自带了Protobuf的RPM包,采用yum直接安装。
- 安装Protobuf。
yum install protobuf protobuf-devel -y
- 安装完成后,查看安装是否完成。
protoc --version
返回内容如下。libprotoc 3.5.0
- 创建测试源文件。
cd /usr/local/src
vi studetn.proto
在“student.proto”插入如下内容:
syntax = "proto3"; message Student { string name = 1; int32 age = 2; }
由于Protobuf 3以及以上版本中默认的参数会自带optional标记,因此上述name和age的定义要去掉optional,否则会报如下错误:
“student.proto:3:13: Explicit 'optional' labels are disallowed in the Proto3 syntax. To define 'optional' fields in Proto3, simply remove the 'optional' label, as fields are 'optional' by default.”。
- 编译和运行测试文件,将“studetn.proto”转化成c++源码。
protoc --cpp_out=./ ./student.proto
ll
回显内容如下,可发现已生成“student.pb.cc”和“student.pb.h”文件。
[root@ecs-xyt ~]# protoc --cpp_out=./ student.proto [root@ecs-xyt ~]# ll total 32 -rw------- 1 root root 13980 Aug 17 17:07 student.pb.cc -rw------- 1 root root 8501 Aug 17 17:07 student.pb.h -rw------- 1 root root 85 Aug 17 16:45 student.proto
父主题: 移植Protobuf
