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/
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
$ 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
相关文章推荐
- protobuf安装配置和编译成java文件教程(for mac)
- protobuf 生成C++文件
- 使用protobuf编写配置文件以及读写
- PHP给C++发协议JSON格式(PROTOBUF环境太难配置)
- protobuf的配置原始文件.proto中,使用修饰符repeated、required、optional生成的java文件的区别
- windows C++ protobuf的文件
- protobuf安装与生成c++文件
- protobuf如何用proto文件导出C++代码文件及链接protobuf静态库问题
- protobuf如何用proto文件导出C++代码文件及链接protobuf静态库问题
- Sublime Text3 protobuf syntax file(语法文件)
- Mac下导出protobuf协议到c#文件
- c/c++写的比较好的读写配置文件
- c++实现读取配置文件
- “基于关键字匹配的文本过滤系统”配置文件的设计和实现(C/C++源码)
- go语言使用protobuf与c++做数据通信。
- C++ 获取当前进程加载模块的完整路径读取配置文件
- caffe中lenet_solver.prototxt配置文件注解
- protobuf c++ 接口分析
- 对protobuf进行ndk编译 config.h文件修改部分
- Interllij protobuf 环境配置