安装(Ubuntu 16.04)

  1. sudo apt-get install autoconf automake libtool curl make g++ unzip
  2. git clone https://github.com/google/protobuf.git
  3. cd protobuf
  4. git submodule update --init --recursive
  5. ./autogen.sh
  6. ./configure
  7. make
  8. make check
  9. sudo make install
  10. sudo ldconfig # refresh shared library cache
  11. protoc --version

如果能查看proto的版本,则代表安装成功,否则失败。

简介

本文主要介绍proto3的使用以及个人理解。关于proto版本的不同以及深入理解,可以参考下面链接。

https://www.jianshu.com/p/5e65f33c4b15?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation    //数据传输格式

proto2

https://www.jianshu.com/p/e89594ecc1db

proto3

https://blog.csdn.net/u011518120/article/details/54604615

用法

proto3

/*****PbTest.proto******/

syntax = "proto3";

package tutorial;

message Person {
string name = 1;
int32 id = 2;
string email = 3; enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
} message PhoneNumber {
string number = 1;
PhoneType type = 2;
} repeated PhoneNumber phones = 4;
} message AddressBook {
Person people = 1;
}
protoc --cpp_out=. PbTest.proto //先编译proto文件生成.cc和.h文件

若对proto文件进行修改,则需要重新使用protoc进行编译,生成新的cc和h文件。

#include <iostream>
#include "PbTest.pb.h" void BookAdd(tutorial::Person *person)
{
person->set_email("fun@qq.com");
person->set_name("fun_name");
person->set_id(1111);
tutorial::Person::PhoneNumber *phone_num=person->add_phones();
phone_num->set_number("1999");
} int main() {
tutorial::Person test;
test.set_id(2111);
test.set_name("main_name");
test.set_email("main@qq.com"); tutorial::Person::PhoneNumber phone_num;
phone_num.set_number("2119");
phone_num.set_type(tutorial::Person::WORK); // tutorial::Person_PhoneNumber phone; //等价上面的phone_num
// phone.set_number("2119");
// phone.set_type(tutorial::Person_PhoneType::Person_PhoneType_HOME);
// std::cout<<phone.number()<<std::endl;
// std::cout<<phone.type()<<std::endl; tutorial::AddressBook book;
BookAdd(book.mutable_people());
// book.mutable_people()->set_name("main2test"); //与BookAdd函数调用等价 std::cout<<"main id :"<<test.id()<<std::endl;
std::cout<<"main name :"<<test.name()<<std::endl;
std::cout<<"main email :"<<test.email()<<std::endl;
std::cout<<"main phone :"<<phone_num.number()<<std::endl;
std::cout<<"main phone type :"<<phone_num.type()<<std::endl; const tutorial::Person &person=book.people();
std::cout<<"AddBook id :"<<person.id()<<std::endl;
std::cout<<"AddBook name :"<<person.name()<<std::endl;
std::cout<<"AddBook email :"<<person.email()<<std::endl;
const tutorial::Person::PhoneNumber &num_phone=person.phones(0);
std::cout<<"AddBook phone :"<<num_phone.number()<<std::endl;
std::cout<<"AddBook phone type:"<<num_phone.type()<<std::endl;
return 0;
}
g++ main.cc PbTest.pb.cc  -lprotobuf -lpthread

输出结果

main id :2111
main name :main_name
main email :main@qq.com
main phone :2119
main phone type :2
AddBook id :1111
AddBook name :fun_name
AddBook email :fun@qq.com
AddBook phone :1999
AddBook phone type:0

总结

    关于proto3的使用过程中,与proto2比较起来,3去掉了字段的限制,以及默认值。虽然说3去掉了[default=value]的操作,但是3在枚举类型中,必须要从0开始,并且枚举的默认值也为0。对于bool类型,默认是false。正因为由于3有了默认值的操作,所以在判断是用户赋予的值还是默认值,则需要话费一些功夫去判断。(由于本人不常用,有需要者,可以百度。)

    如果需要使用oneof字段时,它的原理有点类似与共享(union)结构体。如果数据结构复杂的话,也可以采用c++中的map来存储key-value结构。value也可以是proto中的message类型。

    踩坑:如果字段前面有repeated修饰的话,对其进行修改的时候则需要通过proto对象中的add_()方法对内部嵌套的字段进行赋值。若没有的话,则可以采用 obj.mutable_()->set_()来进行赋值。

若需要参考proto2代码,则可以参考:

https://github.com/protobuf-c/protobuf-c/wiki/Examples

https://github.com/SmallBlackEgg/proto/

C++ProtoBuf的安装与使用的更多相关文章

  1. PHP7中Protobuf的安装使用

    PHP7中Protobuf的安装使用 写这篇文章的缘由是最近在关注RPC框架序列化的一些原理.但是在安装Protobuf的时候,发现网上的教程都太老了,加上目前Protobuf官方已经支持PHP了,不 ...

  2. Protobuf从安装到配置整理帖

    新做的Mini项目计划使用Google的Protobuf来做,关于Protobuf是什么玩意能干什么请自己去看这里:http://code.google.com/p/protobuf/ 这里讲一下安装 ...

  3. protobuf的安装和使用

    以下全部基于win7系统. protobuf是什么,有什么用网上说的已经很多了.这里就是说一下怎么使用.就当给自己做个笔记吧. .proto文件的语法什么的也请网上查看,挺多的. 第一步: 下载pro ...

  4. Google protobuf的安装及使用

    最近应为工作的需要,合作的部门提供了protobuf的接口,总结了一下使用的过程和方法如下: 下载protobuf-2.3.0: http://protobuf.googlecode.com/file ...

  5. Protobuf从安装到配置整理帖 --转

    新做的Mini项目计划使用Google的Protobuf来做,关于Protobuf是什么玩意能干什么请自己去看这里:http://code.google.com/p/protobuf/ 这里讲一下安装 ...

  6. protobuf 编译安装

    1.protobuf是google公司提出的数据存储格式,详细介绍可以参考:https://developers.google.com/protocol-buffers 2.下载最新的protobuf ...

  7. Protobuf的安装使用

    date: 2018-10-12  18:59:13 版权归属原作者,本位转自:https://www.cnblogs.com/autyinjing/p/6495103.html 1. 是什么? Go ...

  8. protobuf编译安装

    为什么选择protobuf,而不选择thift和avro,原因大概几点吧,网上对比的文章很多,我主要关注以下几点 1.protobuf序列化性能最好,序列化后字节数最少. 2.protobuf是单纯的 ...

  9. protobuf文档翻译-安装,数据格式及编码规范

    Install Download protobuf: https://github.com/protocolbuffers/protobuf/releases unzip protoc-3.8.0-l ...

随机推荐

  1. 大型互联网公司分布式ID方案总结

    ID是数据的唯一标识,传统的做法是利用UUID和数据库的自增ID,在互联网企业中,大部分公司使用的都是Mysql,并且因为需要事务支持,所以通常会使用Innodb存储引擎,UUID太长以及无序,所以并 ...

  2. NLP(十四) 情感分析

    情感在自然语言中的表达方式 例句 解释 I am very happy 开心的情感 She is so :( 表达悲伤的图标 import nltk import nltk.sentiment.sen ...

  3. iOS Autoresizing Autolayout Size classes

    Autoresizing:出现最早,仅仅能够针对父控件做约束(注意:要关闭Autolayout&Size classes才能够看到Autoresizing) 代码对应: UIView.h中的a ...

  4. Badboy中创建Suite, test, step和Template

    参考: http://leafwf.blog.51cto.com/872759/1111744 http://www.51testing.com/html/00/130600-1367743.html ...

  5. [code] python+selenium实现打开一个网页

    转载自: http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html http://www.testwo.com/blog/6931 在ec ...

  6. codeforces 810 D. Glad to see you!(二分+互动的输入方式)

    题目链接:http://codeforces.com/contest/810/problem/D 题意:两个人玩一场游戏要猜出Noora选的f种菜的任意两种.一个人猜点另一个人回答 TAK如果 ,(x ...

  7. axios跨域访问eggjs的坑egg-cors egg-passport passport-local session传递问题

    在同一机器上写前端和后端,前端使用webpack-dev-server启动,后端直接在eggjs项目目录下使用npm run dev启动,这种情况下,前端访问后端就是跨域访问.eggjs提供了一个跨域 ...

  8. [译]Introduction to Concurrency in Spring Boot

    当我们使用springboot构建服务的时候需要处理并发.一种错误的观念认为由于使用了Servlets,它对于每个请求都分配一个线程来处理,所以就没有必要考虑并发.在这篇文章中,我将提供一些建议,用于 ...

  9. Cannot create PoolableConnectionFactory (null, message from server: "Host 'admin-PC' is not allowed to connect to this MySQL server")

    1.别人在用自己的tomcat访问我留的查询接口时,出现Cannot create PoolableConnectionFactory (null,  message from server: &qu ...

  10. get和post请求方式的区别

    1.用途方面: get是向服务器请求数据,post是向服务器发送数据. 2.大小方面: get发送数据上有大小限制,post理想上无大小限制,实际上有限制. 3.安全方面: get请求的数据会显示在地 ...