ubuntu 下protobuf安装和使用

一、protobuf安装

1、下载protobuf安装包

https://github.com/google/protobuf.git

2、解压

 3、执行autogen.sh

4、配置

./configure

5、编译

make

6、安装

make install

最后执行

ldconfig # refresh shared library cache.

 

二、使用

1、编写test.proto文件

syntax="proto3";

message Person{
        string name=1;
        int32 id=2;
        string email=3;

        enum PhoneType{
          MOBILE=0;
          HOME=1;
          WORE=2;
        }

        message PhoneNumber{
          string number=1;
          PhoneType type=2;
        }
        repeated PhoneNumber phones=4;
}

message AddressBook{
        Person people=1;
}
~
~
~

2、对proto文件编译生成.cpp文件和.h文件

 sudo protoc --cpp_out=. test.proto

3、创建编写test_proto.cpp文件,测试

 

#include <iostream>
#include "test.pb.h"

using namespace std;

int main() {
        Person test;
        test.set_id(1);
        test.set_name("herry");
        test.set_email("herry@qq.com");
        //Person::PhoneType phonetype;
        Person::PhoneNumber phone_num;
        phone_num.set_number("12345");
        //phone_num.set_type(Person::WORK);

        cout<<"id: "<<test.id()<<endl;
        cout<<"name: "<<test.name()<<endl;
        cout<<"email: "<<test.email()<<endl;
        cout<<"phone number: "<<phone_num.number()<<endl;
        cout<<"phone type: "<<phone_num.type()<<endl;
        return 0;



}

编译输出结果如下所示:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值