This is a creation in Article, where the information may have evolved or changed.
Google Protobuf is a flexible and efficient protocol for serializing data. Compared to XML and JSON formats, PROTOBUF is smaller, faster, and more convenient. Google Protobuf is a cross-language, and comes with a compiler (PROTOC), only need to compile it, can be compiled into Java, Python, C + +, C #, go and other code, and then can be used directly, no need to write other code, from the parsed code. For more detailed information, see: Protocol buffers
PROTOBUF Installation
1, download protobuf code Google/protobuf
2, Installation Protobuf
TAR-XVF Protobuf
CD Protobuf
./configure--prefix=/usr/local/protobuf
Make
Make check
Make install
Now that the installation is complete ^_^, here is the configuration:
(1) Vim/etc/profile, add
Export path= $PATH:/usr/local/protobuf/bin/export pkg_config_path=/usr/local/protobuf/lib/pkgconfig/Save execution, source/ Etc/profile. Also add the above two lines of code in ~/.profile, otherwise the login user cannot find the PROTOC command. (2) Configure the dynamic link library vim/etc/ld.so.conf, add/usr/local/protobuf/lib in the file (note: Add at New Line), and then execute the command: Ldconfig
. proto File
The. Proto file is an important Protobuf file that defines the structure that requires serialization of data. The 3 steps to using PROTOBUF are:
1 defining message Formats in. proto files
2 compiling the. proto file with the Protobuf compiler
3 Write or read messages using the corresponding PROTOBUF API, such as C++/java
Program examples (c + +)
The approximate function of the program example is to define a persion struct and a addressbook that holds the persion, and then a writer writes the struct information to a file, and another program reads the information from the file and prints it to the output.
1 Address.proto File
Package tutorial;message persion { string1; 2 ;} Message addressbook { 1;}
Compile the. proto file, execute the command: protoc-i= $SRC _dir--cpp_out= $DST _dir $SRC _dir/addressbook.proto, execute command protoc--cpp_out=/tmp in the example Addressbook.proto, the files Addressbook.pb.h and addressbook.pb.cc are generated in/tmp.
2 write.cpp file, write AddressBook information to the file, which is a binary
1#include <iostream>2#include <fstream>3#include <string>4#include"Addressbook.pb.h"5 6 using namespacestd;7 8 voidPromptforaddress (Tutorial::P ersion *persion) {9cout <<"Enter persion Name:"<<Endl;Ten stringname; OneCIN >>name; APersion->set_name (name); - - intAge ; theCIN >>Age ; -Persion->Set_age (age); - } - + intMainintargcChar**argv) { - //google_protobuf_verify_version; + A if(ARGC! =2) { atCerr <<"Usage:"<< argv[0] <<"Address_bool_file"<<Endl; - return-1; - } - - Tutorial::addressbook Address_book; - in { -FStream input (argv[1], iOS::inch|ios::binary); to if(!input) { +cout << argv[1] <<": File not found. Creating a new file."<<Endl; - } the Else if(!address_book. Parsefromistream (&input)) { *Cerr <<"Filed to the parse address Book."<<Endl; $ return-1;Panax Notoginseng } - } the + //Add an address A promptforaddress (Address_book.add_persion ()); the + { -FStream Output (argv[1], iOS:: out| Ios::trunc |ios::binary); $ if(!address_book. Serializetoostream (&output)) { $Cerr <<"Failed to write the address book."<<Endl; - return-1; - } the } - Wuyi //Optional:delete All global objects allocated by LIBPROTOBUF. the //Google::p rotobuf::shutdownprotobuflibrary (); - Wu return 0; -}
Compile the Write.cpp file, g++ addressbook.pb.cc write.cpp-o write ' pkg-config--cflags--libs protobuf ' (note, here's the ' sign ' on the keyboard number 1 keys to the left, that is, and ~ is the same key).
3 Read.cpp file, read the AddressBook information from the file and print
1#include <iostream>2#include <fstream>3#include <string>4#include"Addressbook.pb.h"5 6 using namespacestd;7 8 voidListpeople (Consttutorial::addressbook&Address_book) {9 for(inti =0; I < address_book.persion_size (); i++) {Ten ConstTutorial::P ersion& persion =address_book.persion (i); One Acout << persion.name () <<" "<< persion.age () <<Endl; - } - } the - intMainintargcChar**argv) { - //google_protobuf_verify_version; - + if(ARGC! =2) { -Cerr <<"Usage:"<< argv[0] <<"Address_bool_file"<<Endl; + return-1; A } at - Tutorial::addressbook Address_book; - - { -FStream input (argv[1], iOS::inch|ios::binary); - if(!address_book. Parsefromistream (&input)) { inCerr <<"Filed to the parse address Book."<<Endl; - return-1; to } + input.close (); - } the * listpeople (address_book); $ Panax Notoginseng //Optional:delete All global objects allocated by LIBPROTOBUF. - //Google::p rotobuf::shutdownprotobuflibrary (); the + return 0; A}
Compile read.cpp file, g++ addressbook.pb.cc read.cpp-o read ' pkg-config--cflags--libs protobuf '
4 Execution procedures
Reference
1, the installation and use of Google protobuf
2, Google's Open source technology protobuf Introduction and examples
3, Linux installation Protobuf Tutorial + example (detailed)
4, Protobuffer and JSON depth comparison