google protobuf是一个灵活的、高效的用于序列化数据的协议。相比较XML和JSON格式,protobuf更小、更快、更便捷。google protobuf是跨语言的,并且自带了一个编译器(protoc),只需要用它进行编译,可以编译成Java、python、C++、C#、Go等代码,然后就可以直接使用,不需要再写其他代码,自带有解析的代码。更详细的介绍见: Protocol Buffers
protobuf安装
1、下载protobuf代码 google/protobuf
2、安装protobuf
tar -xvf protobuf
cd protobuf
./configure --prefix=/usr/local/protobuf
make
make check
make install
至此安装完成^_^,下面是配置:
(1) vim /etc/profile,添加
.proto文件
.proto文件是protobuf一个重要的文件,它定义了需要序列化数据的结构。使用protobuf的3个步骤是:
1 在.proto文件中定义消息格式
2 用protobuf编译器编译.proto文件
3 用C++/Java等对应的protobuf API来写或者读消息
程序示例(C++版)
该程序示例的大致功能是,定义一个Persion结构体和存放Persion的AddressBook,然后一个写程序向一个文件写入该结构体信息,另一个程序从文件中读出该信息并打印到输出中。
1 address.proto文件
package tutorial; message Persion {
required string name = ;
required int32 age = ;
} message AddressBook {
repeated Persion persion = ;
}
编译.proto文件,执行命令: protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto,示例中执行命令protoc --cpp_out=/tmp addressbook.proto ,会在/tmp中生成文件addressbook.pb.h和addressbook.pb.cc。
2 write.cpp文件,向文件中写入AddressBook信息,该文件是二进制的
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h" using namespace std; void PromptForAddress(tutorial::Persion *persion) {
cout << "Enter persion name:" << endl;
string name;
cin >> name;
persion->set_name(name); int age;
cin >> age;
persion->set_age(age);
} int main(int argc, char **argv) {
//GOOGLE_PROTOBUF_VERIFY_VERSION; if (argc != ) {
cerr << "Usage: " << argv[] << " ADDRESS_BOOL_FILE" << endl;
return -;
} tutorial::AddressBook address_book; {
fstream input(argv[], ios::in | ios::binary);
if (!input) {
cout << argv[] << ": File not found. Creating a new file." << endl;
}
else if (!address_book.ParseFromIstream(&input)) {
cerr << "Filed to parse address book." << endl;
return -;
}
} // Add an address
PromptForAddress(address_book.add_persion()); {
fstream output(argv[], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
return -;
}
} // Optional: Delete all global objects allocated by libprotobuf.
//google::protobuf::ShutdownProtobufLibrary(); return ;
}
编译write.cpp文件,g++ addressbook.pb.cc write.cpp -o write `pkg-config --cflags --libs protobuf` (注意,这里的`符号在键盘数字1键左边,也就是和~是同一个按键)。
3 read.cpp文件,从文件中读出AddressBook信息并打印
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h" using namespace std; void ListPeople(const tutorial::AddressBook& address_book) {
for (int i = ; i < address_book.persion_size(); i++) {
const tutorial::Persion& persion = address_book.persion(i); cout << persion.name() << " " << persion.age() << endl;
}
} int main(int argc, char **argv) {
//GOOGLE_PROTOBUF_VERIFY_VERSION; if (argc != ) {
cerr << "Usage: " << argv[] << " ADDRESS_BOOL_FILE" << endl;
return -;
} tutorial::AddressBook address_book; {
fstream input(argv[], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input)) {
cerr << "Filed to parse address book." << endl;
return -;
}
input.close();
} ListPeople(address_book); // Optional: Delete all global objects allocated by libprotobuf.
//google::protobuf::ShutdownProtobufLibrary(); return ;
}
编译read.cpp文件,g++ addressbook.pb.cc read.cpp -o read `pkg-config --cflags --libs protobuf`
4 执行程序