Centos6.4下安装protobuf及简单使用
Protocol Buffers(简称Protobuf)是Google开发的一种轻量级、高效的结构化数据序列化协议,它具有语言无关、平台无关、可扩展等优点,广泛应用于数据存储、通信协议等领域。在Centos6.4系统下安装和使用Protobuf,可以为开发者提供一种高效的数据处理方式。本文将详细介绍在Centos6.4系统下安装Protobuf的步骤,以及如何进行简单的使用。
目录#
- 安装前准备
- 下载并安装Protobuf
- 验证安装
- 编写并编译Protobuf示例
- 运行示例程序
- 总结
- 参考资料
1. 安装前准备#
在安装Protobuf之前,需要确保系统已经安装了必要的编译工具和依赖库。可以使用以下命令进行安装:
yum install -y gcc-c++ make autoconf automake libtool上述命令会安装GCC编译器、Make工具以及一些必要的自动配置工具。
2. 下载并安装Protobuf#
2.1 下载Protobuf源码#
可以从Protobuf的官方GitHub仓库下载源码包。在撰写本文时,最新的稳定版本是3.19.4,你可以根据实际情况选择合适的版本。
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protobuf-cpp-3.19.4.tar.gz2.2 解压源码包#
使用以下命令解压下载的源码包:
tar -zxvf protobuf-cpp-3.19.4.tar.gz
cd protobuf-3.19.42.3 配置和编译#
在解压后的目录中,执行以下命令进行配置和编译:
./configure
make
make check
make install./configure:该命令会检查系统环境,生成Makefile文件。make:根据Makefile文件编译源码。make check:运行测试用例,确保编译的库和工具正常工作。make install:将编译好的库和工具安装到系统中。
2.4 更新动态链接库缓存#
安装完成后,需要更新动态链接库缓存,使系统能够找到新安装的Protobuf库:
ldconfig3. 验证安装#
安装完成后,可以通过以下命令验证Protobuf是否安装成功:
protoc --version如果输出Protobuf的版本信息,说明安装成功。
4. 编写并编译Protobuf示例#
4.1 编写.proto文件#
创建一个名为addressbook.proto的文件,内容如下:
syntax = "proto3";
package tutorial;
message Person {
string name = 1;
int32 id = 2;
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
}
message AddressBook {
repeated Person people = 1;
}上述.proto文件定义了一个简单的地址簿数据结构,包含人员信息和电话号码信息。
4.2 编译.proto文件#
使用protoc工具编译.proto文件,生成对应的C++代码:
protoc --cpp_out=. addressbook.proto该命令会在当前目录下生成addressbook.pb.h和addressbook.pb.cc两个文件。
4.3 编写示例程序#
创建一个名为write_addressbook.cc的文件,内容如下:
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
// This function fills in a Person message based on user input.
void PromptForAddress(tutorial::Person* person) {
cout << "Enter person ID number: ";
int id;
cin >> id;
person->set_id(id);
cin.ignore(256, '\n');
cout << "Enter name: ";
getline(cin, *person->mutable_name());
cout << "Enter email address (blank for none): ";
string email;
getline(cin, email);
if (!email.empty()) {
person->set_email(email);
}
while (true) {
cout << "Enter a phone number (or leave blank to finish): ";
string number;
getline(cin, number);
if (number.empty()) {
break;
}
tutorial::Person::PhoneNumber* phone_number = person->add_phones();
phone_number->set_number(number);
cout << "Is this a mobile, home, or work phone? ";
string type;
getline(cin, type);
if (type == "mobile") {
phone_number->set_type(tutorial::Person::MOBILE);
} else if (type == "home") {
phone_number->set_type(tutorial::Person::HOME);
} else if (type == "work") {
phone_number->set_type(tutorial::Person::WORK);
} else {
cout << "Unknown phone type. Using default." << endl;
}
}
}
// Main function: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
tutorial::AddressBook address_book;
// Read the existing address book.
fstream input(argv[1], ios::in | ios::binary);
if (!input) {
cout << argv[1] << ": File not found. Creating a new file." << endl;
} else if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -1;
}
// Add an address.
PromptForAddress(address_book.add_people());
// Write the new address book back to disk.
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
return -1;
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}4.4 编译示例程序#
使用以下命令编译示例程序:
g++ -o write_addressbook write_addressbook.cc addressbook.pb.cc -lprotobuf5. 运行示例程序#
编译成功后,可以运行生成的可执行文件:
./write_addressbook addressbook.data程序会提示你输入人员信息,输入完成后,会将信息保存到addressbook.data文件中。
总结#
本文详细介绍了在Centos6.4系统下安装Protobuf的步骤,以及如何编写和编译简单的Protobuf示例程序。通过使用Protobuf,开发者可以高效地进行数据序列化和反序列化,提高数据处理的效率。
参考资料#
- Protobuf官方文档:https://developers.google.com/protocol-buffers
- Protobuf GitHub仓库:https://github.com/protocolbuffers/protobuf