编译前配置
安装依赖库:automake
,autoconf
,libtool
。
解压源码,例如:protobuf-3.11.4.zip
到目录/home/workspace/protobuf-3.11.4
,通过终端进入,输入命令:
./autogen.sh
生成configure
文件。
在父目录创建构建目录,例如:/home/workspace/protobuf-build
,通过终端进入构建目录,输入命令:
./../protobuf-3.11.4/configure --prefix=/opt/protobuf-3.11.4-linux
编译源码
配置成功后,输入命令:make
,成功后,输入命令:make install
,即可把protobuf库安装到目录:/opt/protobuf-3.11.4-linux
。
配置环境变量
vi /etc/profile
打开文件,在末尾添加:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/protobuf-3.11.4-linux/lib/pkgconfig
保存返回,在终端输入命令:sudo ldconfig
。
注意:如果失败则在/etc/ld.so.conf
文件中加入/opt/protobuf-3.11.4-linux/lib
,然后执行sudo ldconfig
。
使用示例
创建协议文件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;
}
在终端输入命令: /opt/protobuf-3.11.4-linux/bin/protoc --cpp_out=. addressbook.proto
生成C++
源码文件:addressbook.pb.h
和addressbook.pb.cc
。
创建示例文件main.cpp
,内容如下:
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
// 填充个人信息, 需要用户输入信息
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;
}
}
}
// 从 AddressBook 中迭代打印个人信息
void ListPeople(const tutorial::AddressBook& address_book) {
for (int i = 0; i < address_book.people_size(); i++) {
const tutorial::Person& person = address_book.people(i);
cout << "Person ID: " << person.id() << endl;
cout << " Name: " << person.name() << endl;
cout << " E-mail address: " << person.email() << endl;
for (int j = 0; j < person.phones_size(); j++) {
const tutorial::Person::PhoneNumber& phone_number = person.phones(j);
switch (phone_number.type()) {
case tutorial::Person::MOBILE:
cout << " Mobile phone #: ";
break;
case tutorial::Person::HOME:
cout << " Home phone #: ";
break;
case tutorial::Person::WORK:
cout << " Work phone #: ";
break;
}
cout << phone_number.number() << endl;
}
}
}
// 主函数
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;
if (argc != 3) {
cerr << "Usage: " << argv[0] << " [0,1](0.read,1.write)" << " ADDRESS_BOOK_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
if (0 == strcmp("0", argv[1])) {
/* 读示例 */
{
// 读取已存在的通讯录文件
fstream input(argv[2], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -1;
}
}
ListPeople(address_book);
} else {
/* 写示例 */
{
// 读取已存在的通讯录文件
fstream input(argv[2], ios::in | ios::binary);
if (!input) {
cout << argv[2] << ": File not found. Creating a new file." << endl;
} else if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -1;
}
}
// 添加通讯信息
PromptForAddress(address_book.add_people());
{
// 保存新添加的通讯信息到本地
fstream output(argv[2], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
return -1;
}
}
}
// Optional: 删除由 libprotobuf 所分配的全局对象
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
错误问题
编译时,需要添加-std=c++11
选项,否则会报各种模板编译不通过。
编译成功后,运行时可能出现以下错误:
[libprotobuf FATAL .././../protobuf-3.11.4/src/google/protobuf/generated_message_util.cc:812] CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning):
terminate called after throwing an instance of 'google::protobuf::FatalException'
what(): CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning):
已放弃 (核心已转储)
这是因为protobuf
里面用到了多线程,需要加上-lpthread
选项,且链接时必须放在protobuf库的后面,例如:
g++ main.cpp addressbook.pb.cc -I/opt/protobuf-3.11.4-linux/include -o test /opt/protobuf-3.11.4-linux/lib/libprotobuf.a -lpthread -std=c++11