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. python——线程与多线程基础

    我们之前已经初步了解了进程.线程与协程的概念,现在就来看看python的线程.下面说的都是一个进程里的故事了,暂时忘记进程和协程,先来看一个进程中的线程和多线程.这篇博客将要讲一些单线程与多线程的基础 ...

  2. PHPCMS V9 非超级管理员批量移动权限

    第一步:确实存在这个问题,修改方法是在后台->扩展->菜单管理里面,管理内容里面添加子菜单, 第二步:在操作完以上步骤后,再在角色权限管理里面再重新赋权限就可以了

  3. iOS:didSelectRowAtIndexPath:方法失效解决办法

    问题描述及解决过程: 为了达到点击TableView空白区退出键盘的效果,给控制器注册了一个UITapGestureRecognizer来识别点击事件,代码如下: #pragma mark - 设置手 ...

  4. H5瀑布流如何实现

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. C++迟后联编和虚函数表

    先看一个题目: class Base { public: virtual void Show(int x) { cout << "In Base class, int x = & ...

  6. Windows下配置nginx+php(wnmp)

      第一部分:准备工作.(系统:Windows 8.1) 1.首先是下载软件. NGINX-1.3.8官网下载:http://nginx.org/en/download.html PHP5.4.8版本 ...

  7. CentOS 7 Mysql yum源

    CentOS 7 安装Mysqlrpm -ivh http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpmyum install m ...

  8. Effective Scala

    Effective Scala Marius Eriksen, Twitter Inc.marius@twitter.com (@marius)[translated by hongjiang(@ho ...

  9. 用Curl测试POST

    POST请求 http://172.16.102.208:8089/wiapi/score?leaderboard_id=1&score=36&app_key=66 目的1:通过脚本发 ...

  10. input框只允许输入数字 --------20160705

    //jquery方法 var num = $(this).val(); num = parseInt(num); if(!num){ $(this).html(''); } $(this).val(n ...