您的位置:首页 > 编程语言 > C语言/C++

C++配置文件之protobuf

2018-01-09 14:49 260 查看
$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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: