protobuf学习、安装、c++练习,适合小白!2021-5-17


前言

本文对protobuf进行简单的介绍,适合新手入门了解练习使用。


一、protobuf语法详解

protobuf是google团队开发的用于高效存储和读取结构化数据的工具。什么是结构化数据呢,正如字面上表达的,就是带有一定结构的数据。比如电话簿上有很多记录数据,每条记录包含姓名、ID、邮件、电话等,这种结构重复出现。
xml、json也可以用来存储此类结构化数据,但是使用protobuf表示的数据能更加高效,并且将数据压缩得更小,大约是json格式的1/10,xml格式的1/20。
protobuf的语法详解可以通过这篇博客学习:https://blog.csdn.net/daaikuaichuan/article/details/105616844?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_baidulandingword-1&spm=1001.2101.3001.4242

二、protobuf安装

先安装依赖项:

yum -y install gcc automake autoconf libtool make

在linux上安装,命令如下:

wget https://github.com/google/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.tar.gz
tar zxvf protobuf-all-3.6.1.tar.gz
./autogen.sh
./configure
make
make install

安装后更改环境变量:

vim /etc/profile
在文件的末尾添加如下的两行:
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/

更改完成之后,执行如下命令立即执行:

source /etc/profile

配置动态链接库路径:

vim /etc/ld.so.conf
在文件中添加/usr/local/protobuf/lib(注意: 在新行处添加)

配置如下图所示:
在这里插入图片描述
更改完成之后,执行如下命令立即执行:

ldconfig

三、c++练习

先编写Person.proto文件如下所示:

syntax = "proto2";
package HUST_Person;

message Person{
	required string name=1;
	enum Gender{
		BOY=0;
		GIRL=1;
	}
	required Gender gender=2;
	required int32 age=3;
	required string ID=4;
	message Home{
		required string country=1;
		required string province=2;
		required string county=3;
		required string addr=4;
	}
	optional Home home=5;
	repeated string phone=6;  
}

执行命令生成c++文件:

// $SRC_DIR: .proto 所在的源目录
// --cpp_out: 生成 c++ 代码
// $DST_DIR: 生成代码的目标目录
// xxx.proto: 要针对哪个 proto 文件生成接口代码
 
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/xxx.proto 
//实例:protoc --cpp_out=./ ./Person.proto

编写测试文件如下:

#include<iostream>
#include<string>
#include"Person.pb.h"

using namespace HUST_Person;

int main(){
    Person person;
    person.set_name("wyl");
    person.set_gender(Person_Gender_BOY);
    person.set_age(25);
    person.set_id("M201870902");
    Person_Home *home=new Person_Home();
    home->set_country("china");
    home->set_province("gansu");
    home->set_county("minxian");
    home->set_addr("**xiang**cun");
    person.set_allocated_home(home);
    person.add_phone("12256783456");
    person.add_phone("11127222345");
    std::cout<<"序列化后:"<<std::endl;
    std::string str=person.SerializeAsString();
    std::cout<<str<<std::endl;
    std::cout<<"反序列化后:"<<std::endl;
    Person copyPerson;
    copyPerson.ParseFromString(str);
    std::cout<<copyPerson.name()<<std::endl;
    std::cout<<copyPerson.gender()<<std::endl;
    std::cout<<copyPerson.age()<<std::endl;
    std::cout<<copyPerson.id()<<std::endl;
    std::cout<<copyPerson.home().country()<<std::endl;
    std::cout<<copyPerson.home().province()<<std::endl;
    std::cout<<copyPerson.home().county()<<std::endl;
    std::cout<<copyPerson.home().addr()<<std::endl;
    int size=copyPerson.phone_size();
    for(int i=0;i<size;++i)
    {
        std::cout<<copyPerson.phone(i)<<std::endl;
    }
    return 0;
}

编译makefile如下:

libdir=/usr/lib/libprotobuf.so.17
cppfile=./Person.pb.cc ./Person_test.cpp

main:
	g++ -g -o test.o -std=c++11 $(cppfile) $(libdir) -lpthread
clean:
	rm -f test.o

最终效果如下:
在这里插入图片描述


总结

本文对protobuf进行了简单介绍,练习的例子可以在我的github中查看。如果想要深入学习,也可以查看官方文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值