1.开发环境
操作系统:Red Hat Enterprise Linux Server release 7.1 (Maipo)
使用语言:C++(本次使用C++语言)
源码版本:https://github.com/protocolbuffers/protobuf/releases/latest
protobuf-cpp-3.9.1.tar.gz
官方安装说明:https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
2.安装步骤
1) 下载源码protobuf-cpp-3.9.1.tar.gz并放置在某一目录下
(以我的目录为例:/home/mylinux/src/)
2)解压并进入源码目录
tar -xvf protobuf-cpp-3.9.1.tar.gz
cd protobuf-cpp-3.9.1
3)编译安装
./configure --prefix=/home/mylinux/software/protobuf
make
make check
make install
安装完毕后,在安装目录(/home/mylinux/software/protobuf)下会生成bin、include和lib三个文件夹。
注意:protobuf默认安装在/usr/local目录下,可使用–prefix参数修改安装路径,本次安装在/home/mylinux/software/protobuf目录下。
4)设置环境变量
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/mylinux/software/protobuf/lib
export LIBRARY_PATH=$LIBRARY_PATH:/home/mylinux/software/protobuf/lib
export PATH=$PATH:/home/mylinux/software/protobuf/bin
检查版本号
protoc --version
3.protobuf简单使用示例
采用protobuf实现文件信息(路径、大小、内容)的获取。
cd /home/mylinux/software/protobuf
1)编写.proto定义文件fileinfo.proto
syntax = "proto3";
package test;
message FileInfo{
string filename = 1;
int32 filesize = 2;
string filecontent = 3;
}
2)编译.proto文件,生成想要的语言的定义及操作文件
protoc --cpp_out=. fileinfo.proto
执行上述指令后,会在fileinfo.proto同级目录生成fileinfo.pb.h和fileinfo.pb.cc
3)使用.proto文件生成的.h和.cc文件
新建一个read.cpp文件
#include "fileinfo.pb.h"
#include
#include
using namespace std;
void PrintFileInfo(const test::FileInfo& file_info){
cout << "filename= " << file_info.filename() << endl;
cout << "filesize= " << file_info.filesize() << endl;
cout << "filecontent= " << file_info.filecontent() << endl;
}
int main(int argc, char* argv[]){
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc < 2){
cerr << "Need more parameters." << endl;
return -1;
}
test::FileInfo file_info;
char* content = NULL;
int fileSize;
{
fstream input(argv[1], ios::in | ios::binary);
if (!input.is_open()){
cerr << "Failed to open file." << endl;
return -1;
}
input.seekg(0, ios::end);
fileSize = input.tellg();
content = new char[fileSize+1];
input.seekg(0, ios::beg);
input.read(content, fileSize);
content[fileSize+1] = '\0';
input.close();
input.clear();
file_info.set_filename(argv[1]);
file_info.set_filesize(fileSize);
file_info.set_filecontent(std::string(content));
delete []content;
}
PrintFileInfo(file_info);
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
使用g++将read.cpp、fileinfo.pb.h和fileinfo.pb.cc编译成可执行文件,g++链接到第三方库时目前采用了直接使用-I和-L参数的方法。
g++ fileinfo.pb.cc read.cpp -o read.out -I /home/mylinux/software/protobuf/include -L /home/mylinux/software/protobuf/lib -lprotobuf -pthread -std=c++11
执行完此命令后,会在read.cpp所在目录下生成read.out文件。
注意:-pthread参数需带上,否则运行read.out时建立protobuf对象就会崩溃,崩溃内容为:
[libprotobuf FATAL google/protobuf/generated_message_util.cc:783] 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):
4)执行read.out
新建/home/mylinux/software/protobuf/test_read_file.txt文件,内容填充为123456789。
执行
./read.out /home/mylinux/software/protobuf/test_read_file.txt
执行结果如下:
filename= /home/mylinux/software/protobuf/test_read_file.txt
filesize= 10
filecontent= 123456789