google protocol 入门 demo
ubunbu系统下google protobuf的安装
说明: 使用protobuf时需要安装两部分:
第一部分为*.proto文件的编译器,它负责把定义的*.proto文件生成对应的c++类的.h和.cpp文件;
第二部分是protobuf的c++动态库(由protobuf的源码编译生成),该部分在生成链接生成可执行文件时需要使用到。
1. 安装编译google protobuf源文件时需要的依赖文件
sudo apt-get install autoconf automake libtool curl make g++ unzip
2. 下载google protobuf 的c++对应的源码,并解压至当前目录中
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.2/protobuf-cpp-3.11.2.tar.gz
tar -xf protobuf-cpp-3.11.2.tar.gz
3. 进入解压后的目录,并安装
./configure
make
make check
sudo make install
sudo make ldconfig
google protobuf的使用
1. 新建一个表示电话薄的addressbook.proto文件
touch addressbook.proto
echo 'syntax = "proto2";
package tutorial;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phones = 4;
}
message Addressbook {
repeated Person people = 1;
}
' > addressbook.proto
2. 使用porotc编译器生成address.proto对应的*.cpp和*.h文件,运行以下命令生成了addressbook.pb.cc和addressbook.pb.h文件。
protoc addressbook.proto
3. 创建两个读与写电话薄的*.cpp文件,使用google protobuf完成写与读的功能。
- 写文件:
touch write_message.cpp
echo '#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
void PromptForAddress(tutorial::Person* person) {
cout << "Enter person ID number: ";
int id;
cin >> id;
person->set_id(id);
cin.ignore(256, '\n');
cout << "Enter name: ";
getline(cin, *person->mutable_name());
cout << "Enter email address (blank for none): ";
string email;
getline(cin, email);
if (!email.empty())
person->set_email(email);
while (true) {
cout << "Enter a phone number (or leave blank for finish): ";
string number;
getline(cin, number);
if (number.empty())
break;
tutorial::Person::PhoneNumber* phone_number = person->add_phones();
phone_number->set_number(number);
cout << "Is this a mobile, home, or work phone? ";
string type;
getline(cin, type);
if (type == "mobile")
phone_number->set_type(tutorial::Person::MOBILE);
else if (type == "home")
phone_number->set_type(tutorial::Person::HOME);
else if (type == "work")
phone_number->set_type(tutorial::Person::WORK);
else
cout << "Unkown phone type. Using default. " << endl;
}
}
int main(int argc, char* argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << "ADDRESS_BOOK_FILE " << endl;
return -1;
}
tutorial::Addressbook address_book;
fstream input(argv[1], ios::in | ios::binary);
if (!input)
cout << argv[1] << ": File not found. Creating a new file. " << endl;
else if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book. " << endl;
return -1;
}
PromptForAddress(address_book.add_people());
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book. " << endl;
return -1;
}
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
' > write_message.cpp
- 读文件:
touch read_message.cpp
echo '#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
void ListPeople(const tutorial::Addressbook& address_book) {
for (int i = 0; i < address_book.people_size(); ++i) {
const tutorial::Person& person = address_book.people(i);
cout << "Person ID: " << person.id() << endl;
cout << "Name: " << person.name() << endl;
if (person.has_email()) {
cout << " E-mail address: " << person.email() << endl;
}
for (int j = 0; j < person.phones_size(); ++j) {
const tutorial::Person::PhoneNumber& phone_number = person.phones(j);
switch (phone_number.type()) {
case tutorial::Person::MOBILE:
cout << " Mobile phone#: ";
break;
case tutorial::Person::HOME:
cout << " Home phone#: ";
break;
case tutorial::Person::WORK:
cout << " Work phone#: ";
break;
}
cout << phone_number.number() << endl;
}
}
}
int main(int argc, char** argv) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << "ADDRESS_BOOK_FILE " << endl;
return -1;
}
tutorial::Addressbook address_book;
fstream input(argv[1], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book. " << endl;
return -1;
}
ListPeople(address_book);
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
' > read_message.cpp
4. 分别编译write_message.cpp文件和read_message.cpp文件(其中pkg-config命令可以输出关于protobuf动态库相关的编译时的参数)
g++ write_message.cpp addressbook.pb.cc -o write.out $(pkg-config --cflags --libs protobuf)
g++ read_message.cpp addressbook.pb.cc -o read.out $(pkg-config --cflags --libs protobuf)
5. 分别运行write.out和read.out文件进行写与读操作
./write.out result.protobuf
./read.out result.protobuf
google protocol 入门 demo的更多相关文章
- Google Protocol Buffers 快速入门(带生成C#源码的方法)
Google Protocol Buffers是google出品的一个协议生成工具,特点就是跨平台,效率高,速度快,对我们自己的程序定义和使用私有协议很有帮助. Protocol Buffers入门: ...
- Google Protocol Buffers 入门
Google Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的 ...
- Google Protocol Buffer入门
简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...
- Google Protocol Buffers介绍
简要介绍和总结protobuf的一些关键点,从我之前做的ppt里摘录而成,希望能节省protobuf初学者的入门时间.这是一个简单的Demo. Protobuf 简介 Protobuf全称Google ...
- Google Protocol Buffer 的使用和原理[转]
本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...
- 基于springboot构建dubbo的入门demo
之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...
- canal入门Demo
关于canal具体的原理,以及应用场景,可以参考开发文档:https://github.com/alibaba/canal 下面给出canal的入门Demo (一)部署canal服务器 可以参考官方文 ...
- Google protocol buffer的配置和使用(Linux&&Windows)
最近自己的服务器做到序列化这一步了,在网上看了下,序列化的工具有boost 和google的protocol buffer, protocol buffer的效率和使用程度更高效一些,就自己琢磨下把他 ...
- Google Protocol Buffer 的使用和原理(无论对存储还是数据交换,都是个挺有用的东西,有9张图做说明,十分清楚)
感觉Google Protocol Buffer无论对存储还是数据交换,都是个挺有用的东西,这里记录下,以后应该用得着.下文转自: http://www.ibm.com/developerworks/ ...
随机推荐
- C++中字符常量与字符常量不能直接相加
定义string变量,并进行初始化,如下: string s1 = "Hello"; string s2 = s1 + "World"; string s3 = ...
- 特殊符号unicode编码
包括箭头类.基本形状类.货币类.数学类.音乐符号类.对错类.星星类.星座类.国际象棋类.扑克牌类.希腊字母.十字类.法律符号.标点符号,详情见以下网址:http://caibaojian.com/un ...
- 【Mac电脑新手技巧】苹果电脑如何更换用户头像?
想给Mac电脑换一个喜欢的用户头像?苹果电脑的用户头像如何更换? 对于很多Mac小白来说,给自己的Mac换一个可心的用户头像很是必要.但是,大多数Mac新手都觉得无从下手!如果你也想给自己的Mac换一 ...
- AGC018F - Two Trees
题意 有两棵节点数均为 n 的有根树,你需要构造一个序列 \(X_1,X_2,...,X_n\).使得对于每一棵树的每一个节点, 若令它所有的后代(包括它本身)为 \(a_1,a_2,...,a_k\ ...
- 逻辑卷管理(LVM)-迁移
逻辑卷管理(LVM)-迁移 更换卷组中逻辑卷中的一块硬盘流程:1确保卷组剩余空间大于需要更换的空间(缩减或添加添加新空间)-2迁移-3从卷组删除-4删除物理卷 #移除sdc1 1.查看卷组可用空间是否 ...
- 前后端分离-crud&svn
前后端分离-crud&svn 1. 跨域 1.1 什么是跨域 请求方与服务方的源不同,即跨域,包括: 1. 协议不同 2. 域名不同 3. 端口不同 1.2 跨域不一定存在跨域问题 什么情况下 ...
- WPF 控件功能重写(ComboBox回车搜索)
前言:在我们日常使用软件的时候,Combobox会让用户很方便的选择出需要的东西,但是ComboBox中的下拉行数过多时就不那么好用了. 如果在项目中有很多这样的ComboBox控件的话,我们可以考虑 ...
- P2919 [USACO08NOV]守护农场Guarding the Farm
链接:P2919 ----------------------------------- 一道非常暴力的搜索题 ----------------------------------- 注意的是,我们要 ...
- maven的核心概念——坐标
7.1 几何中的坐标 [1]在一个平面中使用x.y两个向量可以唯一的确定平面中的一个点. [2]在空间中使用x.y.z三个向量可以唯一的确定空间中的一个点. 7.2 Maven的坐标 使用如下三个向量 ...
- TP框架上传图片至阿里云oss
首先安装阿里云oss扩展: composer require aliyuncs/oss-sdk-php 如果这个安装不上可以直接下载SDK的包: 链接:https://pan.baidu.com/s/ ...