1.Protocol Buffers简介

Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储、通信协议等方面。现阶段支持C++、JAVA、Python等三种编程语言。

2.protobuf相比Xml的优点

•更简单
•数据描述文件只需原来的1/10至1/3
•解析速度是原来的20倍至100倍
•减少了二义性
•生成了更容易在编程中使用的数据访问类
3.安装
yum -y install  protobuf-compiler protobuf-static protobuff protobuf-devel
4.使用
vi helloworld.proto
 
输入下面的数据:

message helloworld {

required int32 id = 1; // ID

required string str = 2; // str

}

5.编译 .proto

protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/helloworld.proto

protoc -I=. --cpp_out=. ./helloworld.proto

命令将生成:

helloworld.pb.h , 定义了 C++ 类的头文件

helloworld.pb.cc , C++ 类的实现文件

6.测试程序

#include "helloworld.pb.h" //包含生成的头文件
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
helloworld msg;
msg.set_id(101);
msg.set_str("hello");
// 序列化消息
char buff[1024] = {0};
msg.SerializeToArray(buff, 1024);
//解析消息
helloworld msgread;
msgread.ParseFromArray(buff, 1024);
cout << msgread.id() << endl;
cout << msgread.str() << endl;
}

  

  

 7.编译运行

g++ -o main  main.cpp helloworld.pb.cc -lprotobuf -lpthread

./main

 

protobuf简介和使用的更多相关文章

  1. Protobuf(一)——Protobuf简介

    Protobuf简介 ​ 什么是 Google Protocol Buffer? 假如您在网上搜索,应该会得到类似这样的文字介绍: ​ Google Protocol Buffer( 简称 Proto ...

  2. 消息中间件NetMQ结合Protobuf简介

    概述 对于稍微熟悉这两个优秀的项目来说,每个内容单独介绍都不为过,本文只是简介并探讨如何将两部分内容合并起来,使其在某些场景下更适合.更高效. NetMQ:ZeroMQ的.Net版本,ZeroMQ简单 ...

  3. protobuf简介

    #1,简介 把某种数据结构的信息,以某种格式保存起来: 主要用于数据存储,传输协议格式. #2,优点 性能好 反观XML的缺点:解析的开销惊人,不适用于事件性能敏感的场合:为了有较好的可读性,引入一些 ...

  4. Google 的开源技术protobuf 简介与例子

    本文来自CSDN博客:http://blog.csdn.NET/program_think/archive/2009/05/31/4229773.aspx 今天来介绍一下"Protocol  ...

  5. [转]Google 的开源技术protobuf 简介与例子

    本文来自CSDN博客:http://blog.csdn.NET/program_think/archive/2009/05/31/4229773.aspx 今天来介绍一下“Protocol Buffe ...

  6. Protobuf 简介及简单应用

    Protobuf 是 protocol buffers 的缩写. 根据官网的说法, protocol buffers 与平台无关, 与语言无关, 实现数据序列化的一种手段. 正如名字一样, proto ...

  7. 【转】深入 ProtoBuf - 简介

    之前在网络通信和通用数据交换等应用场景中经常使用的技术是 JSON 或 XML,而在最近的开发中接触到了 Google 的 ProtoBuf. 在查阅相关资料学习 ProtoBuf 以及研读其源码之后 ...

  8. google protobuf安装与使用

    google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...

  9. Google 开源技术protobuf

    http://blog.csdn.net/hguisu/article/details/20721109#0-tsina-1-1601-397232819ff9a47a7b7e80a40613cfe1 ...

随机推荐

  1. Virtualbox安装USB2.0/3.0

    系统:Ubuntu16.04 软件:Virtualbox5.1 1.打开Virtualbox,不启动虚拟系统. 2.点击设置->USB->启动usb2.0. 3.若发现不能启用,则到官网下 ...

  2. AX2012修改properties字体

    参考自http://www.ithao123.cn/wenku/list_310_2.html static void GD_Eric_ChangeUserinfoFont(Args _args){  ...

  3. C++ 在容器中存放函数指针

    注意,对一般c++ 98标准编译器而言,容器泛型模板是不支持直接存放函数指针的.需要typedef将函数指针重命名. 比如,一个void返回值参数也为void的函数指针,需要 typedef void ...

  4. Linux Shell 学习总结

    1. -bash: ./dd.sh: /bin/bash^M: bad interpreter: 没有那个文件或目录 当出现上面这问题的时候,首先看你的.profile 里面是否配置对了,一般配置为: ...

  5. 用PHP解析类JSON字符串为数组的实现

    题目:把字符串嵌套关系转换成数组,字符串只包含成对中括号.数字和逗号字符串:(1,(1,2,(1,(1,2,(1)),3)),3,(1,(1,2,((1((1,(1,2,(1,2,3),4,5),3) ...

  6. Docker私服仓库push 错误

    docker push 提交时错误提示: docker push 192.168.1.110:5000/redis Error response from daemon: invalid regist ...

  7. html 学习(一)

    一.用CSS实现如下布局,使用三个div 代码实现如下: 说明: 1.margin-left:110px; _margin-left:107px; margin-left:110px; 所有浏览器都要 ...

  8. hdu 1159, LCS, dynamic programming, recursive backtrack vs iterative backtrack vs incremental, C++ 分类: hdoj 2015-07-10 04:14 112人阅读 评论(0) 收藏

    thanks prof. Abhiram Ranade for his vedio on Longest Common Subsequence 's back track search view in ...

  9. C语言学习1——结构体剖析

    一、定义结构体变量的方法 1.1先声明结构体类型在定义变量名 例如: a.声明结构体类型 struct student { int num; char name[20]; char sex; int ...

  10. 例子:Bluetooth app to device sample

    本例子演示了: 判断蓝牙是否打开,是通过一个HRsult值为0x8007048F的异常来判断的 catch (Exception ex) { if ((uint)ex.HResult == 0x800 ...