C++配置文件之protobuf

本文介绍了如何在C++中使用protobuf,包括从GitHub克隆protobuf库,安装依赖,编译和安装protobuf,以及提供了protobuf的学习资源链接,如C++、Python的教程和语法解析。

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

$ cd ./protobuf/src  # compilec++ only

$ sudo apt-get install autoconf automake libtool curl make g++ unzip

$ ./autogen.sh

$ ./configure

$ make

$ make check

$ sudo make install

$ sudo ldconfig # refresh shared library cache

 

protobuf更小、更快、更简单,可以自定义数据结构,“向后”兼容性好。

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

英文学习文档:https://developers.google.com/protocol-buffers/docs/cpptutorial

中文c++学习文档:https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html

中文python学习文档:https://www.jianshu.com/p/5ea08c6b7031

语法解析:http://colobu.com/2015/01/07/Protobuf-language-guide/

~/Desktop/test_protobuf$ ls
build  build.sh  compile.sh  lm.helloworld.proto  run.sh  test_reader.cpp  test_writer.cpp

syntax = "proto2";

package lm;
message helloworld {
   required int32     id = 1;  // ID 
   required string    str = 2;  // str 
   optional int32     opt = 3;  //optional field 
}


message Person {
   required string name = 1;
   required int32 id = 2;        // Unique ID number for this person. 
   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 phone = 4;
}
build.sh :     protoc --proto_path=./ --cpp_out=./build lm.helloworld.proto

#include "lm.helloworld.pb.h"
#include <fstream>
#include <iostream>

int main(void)
{
    lm::helloworld msg1;
    msg1.set_id(101);
    msg1.set_str("hello");

    // Write the new address book back to disk. 
    std::fstream output("./build/log", std::ios::out | std::ios::trunc | std::ios::binary);

    if (!msg1.SerializeToOstream(&output)) {
        std::cout << "Failed to write msg." << std::endl;
    }
    else {
        std::cout << "Write Done!" << std::endl;
    }

    lm::Person person;
    person.set_name("changqing");
    person.set_id(123);
    person.set_email("123@123.com");
    lm::Person::PhoneNumber* pn = person.add_phone();
    pn->set_number("qwe");

    if (!person.SerializeToOstream(&output)) {
        std::cout << "Failed to write msg." << std::endl;
    }
    else {
        std::cout << "Write Done!" << std::endl;
    }

    return 0;
}
compile.sh :   g++ test_writer.cpp ./build/lm.helloworld.pb.cc -o ./build/test_writer -I./build/ -I/usr/local/include -L/usr/local/lib -lprotobuf

#include "lm.helloworld.pb.h"
#include <fstream>
#include <iostream>

void ListMsg(const lm::helloworld & msg)
{
    std::cout << "Read : " << msg.id() << std::endl;
    std::cout << "Read : " << msg.str() << std::endl;
}

int main(int argc, char* argv[])
{
    lm::helloworld msg1;
    std::fstream input("./build/log", std::ios::in | std::ios::binary);
    if (!msg1.ParseFromIstream(&input)) {
        std::cout << "Failed to parse address book." << std::endl;
        return -1;
    }
    input.close();

    ListMsg(msg1);

    lm::Person person;
    std::fstream input2("./build/log", std::ios::in | std::ios::binary);
    if (!person.ParseFromIstream(&input2)) {
        std::cout << "Failed to parse address book." << std::endl;
        return -1;
    }
    input2.close();

    std::cout << "Read : " << person.name() << std::endl;
    std::cout << "Read : " << person.id() << std::endl;
    std::cout << "Read : " << person.email() << std::endl;
    lm::Person::PhoneNumber pn = person.phone(0);
    std::cout << "Read : " << pn.number() << std::endl;

    return 0;
}
compile.sh :   g++ test_reader.cpp ./build/lm.helloworld.pb.cc -o ./build/test_reader -I./build/ -I/usr/local/include -L/usr/local/lib -lprotobuf

run.sh :      ./build/test_writer        ./build/test_reader

结果如下:

Write Done!
Write Done!
Read : 101
Read : hello
Read : changqing
Read : 123
Read : 123@123.com
Read : qwe


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值