【C++】Google Protocol Buffer(protobuf)详解(一)
1、简介
Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。
源码下载:
github地址 : https://github.com/protocolbuffers/protobuf
编译:
tar zxvf protobuf*.tar.gz
cd protobuf*/
./configure –prefix=/usr/local/protobuf
make
make check
make install
protoc –version
参考博客:
https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html
https://www.cnblogs.com/tohxyblog/p/8974763.html
https://blog.csdn.net/u014308482/article/details/52958148
2、ProtoBuf协议说明
proto文件定义了协议数据中的实体结构(message ,field)
关键字message: 代表了实体结构,由多个消息字段(field)组成。
消息字段(field): 包括数据类型、字段名、字段规则、字段唯一标识、默认值
字段规则:
required:必须初始化字段,如果没有赋值,在数据序列化时会抛出异常
optional:可选字段,可以不必初始化。
repeated:数据可以重复(相当于java 中的Array或List)
字段唯一标识:序列化和反序列化将会使用到。
数据类型:
3、ProtoBuf的使用流程
1> 定义.proto文件
网上都有如下的例子说明,皆出自IBM的学习社区:https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html
proto文件:first.proto
package lm;
message helloworld
{ required int32 id = 1; // ID required string str = 2; // str optional int32 opt = 3; //optional field
}
2> 生成*.pb.cc 和 *.pb.h
protoc命令格式:protoc -I=<proto文件所在路径> --cpp_out=<输出路径> proto文件名
如:protoc -I=. --cpp_out=. first.proto
将会生成:first.pb.cc first.pb.h
3> 序列化操作
将helloworld结构 序列化到文件log中:firstWrite.cpp
#include "first.pb.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main(void)
{ lm::helloworld msg1; msg1.set_id(101); msg1.set_str("hello");fstream output("./log", ios::out | ios::trunc | ios::binary);if (!msg1.SerializeToOstream(&output)) {cout << "Failed to write msg." << endl;return -1;}return 0;
}
编译时需要链接protobuf库,并且使用first.pb.cc一起编译:
g++ firstWrite.cpp first.pb.cc -lprotobuf
4> 反序列化操作
从文件中,读取到helloworld结构中: firstRead.cpp
#include "first.pb.h"
#include <string>
#include <fstream>
#include <iostream>using namespace std;int main(void)
{ lm::helloworld msg; msg.set_id(101); msg.set_str("hello");fstream input("./log", ios::in | ios::binary);if (!msg.ParseFromIstream(&input)) {cout << "Failed to parse address book." << endl;return -1;}cout << msg.id() << endl;cout << msg.str() << endl;return 0;
}
编译时需要链接protobuf库,并且使用first.pb.cc一起编译:
g++ firstRead.cpp first.pb.cc -lprotobuf
5> cmake编译CMakeLists.txt(注意修改cmake版本)
cmake_minimum_required(VERSION 3.14.1)
project (protobuf_test)set (PROJECT_LINK_LIBS libprotobuf.so) add_executable(firstWrite firstWrite.cpp first.pb.cc)
add_executable(firstRead firstRead.cpp first.pb.cc)target_link_libraries(firstWrite ${PROJECT_LINK_LIBS} )
target_link_libraries(firstRead ${PROJECT_LINK_LIBS} )
【C++】Google Protocol Buffer(protobuf)详解(一)相关推荐
- Protocol Buffer技术详解(语言规范)
该系列Blog的内容主体主要源自于Protocol Buffer的官方文档,而代码示例则抽取于当前正在开发的一个公司内部项目的Demo.这样做的目的主要在于不仅可以保持Google文档的良好风格 ...
- Protocol Buffer技术详解(C++实例)
原文:http://www.cnblogs.com/stephen-liu74/archive/2013/01/04/2842533.html 这篇Blog仍然是以Google的官方文档为主线,代码实 ...
- 前端后台以及游戏中使用Google Protocol Buffer详解
前端后台以及游戏中使用Google Protocol Buffer详解 前端后台以及游戏中使用Google Protocol Buffer详解 0.什么是protoBuf 1.下载protobuf的编 ...
- 由Google Protocol Buffer的小例子引起的g++编译问题
问题 学习 Google Protocol Buffer 的使用和原理时,提供了一个小例子,讲述了protobuf的使用方法. 假如已经有了如下文件: 其中writer.cpp如下: #include ...
- Google Protocol Buffer 的使用和原理
FROM : https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Google Protocol Buffer 的使用和原理 Protocol ...
- Google Protocol Buffer 的使用和原理(c++)
简介 什么是 Google Protocol Buffer? 假如您在网上搜索,应该会得到类似这样的文字介绍: Google Protocol Buffer( 简称 Protobuf) 是 Googl ...
- windows下Google Protocol Buffer 编译安装(vs)教程
蒲公英的博客 分享技术带来的快乐 Kuaile.IN 搜索 首页 安卓相关 WordPress 主机相关 Linux相关 网络技术 电脑技术 编程技术 免费资源 当前位置: 首页 > 编程技术 ...
- c++ java通信 protocol buffer,google protocol buffer (C++,Java序列化应用实例)
google protocol buffer (C++,Java序列化使用实例) 转载,请注明出处: http://blog.csdn.net/eclipser1987/article/details ...
- C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
From: http://www.jb51.net/article/82795.htm 这篇文章主要介绍了C#使用Protocol Buffer(ProtoBuf)进行Unity的Socket通信的实 ...
最新文章
- linux动态库命名规则
- jquery将html转为pdf文件,通过Jquery将HTML Div转换为PDF
- java获取eureka_获取Eureka服务列表的各种场景
- Hadoop SSH免密登录公钥生成并实现不同主机间的免密登录
- swift调用支付宝
- 《MySQL——使用联合索引、覆盖索引,避免临时表的排序操作》
- g标签 怎么设置svg_SVG g元素
- Java 工具包收藏
- PotPlayer:最强播放器,无边框
- 【转】TranslateAnimation详解
- wxPython 资料链接
- 动手学深度学习Pytorch Task08
- 比大小 log_2^3 与 log_3^5
- 电信行业BOSS系统
- Cisco设备基础命令
- ArcGIS土地利用转移矩阵(附土地利用数据下载)
- 随机点名和抽题软件(可支持ppt扩展模式使用)
- python中的for什么意思_python中的for是什么
- 令狐冲和TCP/IP协议的第三层协议的关系(经典)
- 国家示范性高职院校名单(109所)
热门文章
- Opencv中的FaceRecognizer类
- 网络流最大流EK算法板子
- 机器学习(16)ROC曲线与AUC指标(癌症分类的模型检测--AUC指标)
- 机器学习(13)岭回归(线性回归的改进)
- 【python】图像映射:单应性变换与图像扭曲
- ADPRL - 近似动态规划和强化学习 - Note 2 - Stochastic Finite Horizon Problem
- 关于std::string 在 并发场景下 __grow_by_and_replace free was not allocated 的异常问题
- ceph中使用ceph-objectstore-tool将pg从incomplete标记为complete
- 快过高铁!构建云分布式应用还能这样操作?!
- ERROR: from PIL import Image ImportError: No module named PIL