CentOS7安装protobuf(C++)和简单使用

本文档详细介绍了在CentOS7上下载、安装protobuf 3.11.4的过程,包括解决autoreconf命令找不到的问题。在安装完成后,通过一个简单的.proto文件示例,展示了如何生成C++代码并编写程序进行序列化和反序列化操作。在程序运行中需要注意,protobuf3可能需要链接libprotobuf-lite库以避免运行时崩溃。

下载 protobuf

下载地址

使用wget下载,或者手动下载好FTP传到Linux上

在Linux 64位环境下进行编译

我下载的是protobuf-all-3.11.4.tar.gz 包

首先解压

tar zxvf protobuf-all-3.11.4.tar.gz

进入解压目录

cd protobuf-3.11.4/

安装 protobuf

此时可能会遇到报错,如:autoreconf: command not found

则说明需要安装几个软件

sudo yum install autoconf 
sudo yum install automake 
sudo yum install libtool

以上安装成功后再次执行

./autogen.sh 

生成编译配置文件成功

运行配置脚本

./configure

make

sudo    #输入密码
make  #要编译很久
make check    #测试
make install    #安装

查看版本

protoc --version    #查看版本

sw708db3kq.png?q-sign-algorithm=sha1&q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&q-sign-time=1584805917;1584813117&q-key-time=1584805917;1584813117&q-header-list=&q-url-param-list=&q-signature=a61bc28097ee40e9e307d93c1080fc3c61ef263a

注:新版本不需要执行autogen.sh脚本,直接./configure就行,./configure不用添加--prefix,默认位置就在/usr/local/

简单使用protobuf

创建一个.proto文件:addressbook.proto,内容如下

syntax = "proto3";
package IM;
message Account {
    //账号
    uint64 ID = 1;
    //名字
    string name = 2;
    //密码
    string password = 3;
}

message User {
    Account user = 1;
}

编译.proto文件,生成C++语言的定义及操作文件

protoc --cpp_out=. Account.proto

生成的文件:Account.pb.h, Account.pb.cc

1im4otjmxl.png?q-sign-algorithm=sha1&q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&q-sign-time=1584812576;1584819776&q-key-time=1584812576;1584819776&q-header-list=&q-url-param-list=&q-signature=3834c0e2b55605cebcdfee268ec0605d7df238ec

编写程序main.cpp

#include <iostream>
#include <fstream>
#include "Account.pb.h"

using namespace std;

int main(int argc, char** argv)
{
    IM::Account account1;
    account1.set_id(1);
    account1.set_name("windsun");
    account1.set_password("123456");

    string serializeToStr;
    account1.SerializeToString(&serializeToStr);
    cout <<"序列化后的字节:"<< serializeToStr << endl;


    IM::Account account2;
    if(!account2.ParseFromString(serializeToStr))
    {
        cerr << "failed to parse student." << endl;
        return -1;
    }
    cout << "反序列化:" << endl;
    cout << account2.id() << endl;
    cout << account2.name() << endl;
    cout << account2.password() << endl;

    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

编译

g++ main.cpp Account.pb.cc -o main -lprotobuf -std=c++11 -lpthread

注:程序使用protobuf,编译没有问题,运行时一到建立protobuf对象就崩溃,搜索了半天没找到原因,后来偶然看到以前正常使用的makefile文件中后面加了-lpthread,加上就好了。我自己的程序没有用到多线程,应该是protobuf3里面用到了。

运行

akrhz2wynf.png?q-sign-algorithm=sha1&q-ak=AKID2uZ1FGBdx1pNgjE3KK4YliPpzyjLZvug&q-sign-time=1584812788;1584819988&q-key-time=1584812788;1584819988&q-header-list=&q-url-param-list=&q-signature=1b49effb28e3d17aebbf51a52ff37ad8d714a1f5

可以看到能正常序列化到string,并能反序列化。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值