protobuf初体验

本文介绍了在Red Hat Enterprise Linux Server 7.1上使用C++进行protobuf的安装和基本使用。首先下载protobuf-3.9.1源码,解压并编译安装到指定目录。接着,通过.proto文件生成C++代码,并创建一个read.cpp文件进行实例操作。在编译时需链接protobuf库,并加入-pthread参数。最后,通过执行编译后的程序读取文件信息。
摘要由CSDN通过智能技术生成

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 <fstream>
#include <iostream>
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值