ubuntu下protobuf安装使用(详解)
1.安装
1.1安装前的环境
以下几个库都有安装
sudo apt-get install autoconf automake libtool curl make g++ unzip
1.2安装
注意:以下命令在超级用户下执行
sudo apt-get install autoconf automake libtool curl make g++ unzip
git clone https://github.com/google/protobuf.git #安装子模块
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
# 如果没有克隆子模块,make check会失败但是可以继续make install,但是使用某些功能时可能会出错make check
sudo make install
sudo ldconfig # refresh shared library cache.
1.2.1安装完成之后,配置protobuf命令
更改环境变量:
vim /etc/profile
在文件的末尾添加如下的两行:
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
更改完成之后,执行如下命令立即执行:
source /etc/profile
这一步是必须的,因为如果少了这一步,会出现找不到protoc的命令错误。
配置动态链接库
vim /etc/ld.so.conf
在文件中添加/usr/local/protobuf/lib(注意: 在新行处添加)
更改完成之后,执行如下命令立即执行:
ldconfig
1.3.安装成功
protoc --version
1.4.卸载
sudo apt-get remove libprotobuf-dev
2.如何使用ProtoBuf
创建Person.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 phone = 4;
}
message AddressBook
{
repeated Person person =1;
}
现在将定义的协议转化为我们编程语言能够识别的文件
#查看里面的命令
protoc --help
#输出文件
protoc Person.proto --cpp_out=.
然后定义一个源文件,用来测试
main.cpp
#include "Person.pb.h"
#include <fstream>
#include <iostream>
using namespace std;
void PromptForAddress(tutorial::Person*);
int main(int argc, char* argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if(2 != argc)
{
//必须指定电话本名称才执行程序
cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
fstream in("ADDRESS_BOOK_FILE", ios::binary | ios::in);
if(!in)
{
cerr << "open file ADDRESS_BOOK_FILE failed!\n";
return -1;
}
if(!address_book.ParseFromIstream(&in))
{
cerr << "Parse File ADDRESS_BOOK_FILE failed!\n";
return -1;
}
in.close();
//增加一个Person,可通过多次调用该接口增加联系人
//具有repeated的属性可通过add_fieldname方法增加一个属性
PromptForAddress(address_book.add_person());
fstream out("ADDRESS_BOOK_FILE", ios::binary | ios::out | ios::trunc);
if(!address_book.SerializeToOstream(&out))
{
cerr << "Failed to Write Address Book!\n";
return -1;
}
//可选的,回收所有ProtoBuf分配的对象
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
void PromptForAddress(tutorial::Person* person)
{
cout<<"Enter a Person ID number: ";
int id;
cin >> id;
person->set_id(id);
/*忽略CIN的前256个字符,或者忽略CIN的换行符之前的字符,包括换行符
这样的话不会将换行符之前的其他类型的数据保留在输入缓冲中
*/
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_phone();
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;
phone_number->set_type(tutorial::Person::HOME);
}
}
}
然后编译即可,但是编译千万不要忘记加库
g++ main.cpp Person.pb.cc -lprotobuf
更多推荐
所有评论(0)