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


    下载 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    #查看版本
    

    在这里插入图片描述
    注:新版本不需要执行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
    在这里插入图片描述
    编写程序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里面用到了。

    运行
    在这里插入图片描述
    可以看到能正常序列化到string,并能反序列化。
    注:如果出现了错误

    error while loading shared libraries: libprotobuf.so.22: cannot open shared object file: No such file or directory
    

    方法1:请执行export LD_LIBRARY_PATH=/usr/local/lib
    方法2:root用户编辑/etc/ld.so.conf中加入/usr/local/lib这一行,保存之后,再运行ldconfig更新一下配置即可。

  • 相关阅读:
    A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
    Fliptile 开关问题 poj 3279
    Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276
    Aggressive cows 二分不仅仅是查找
    Cable master(二分题 注意精度)
    B. Pasha and String
    Intervals poj 1201 差分约束系统
    UITextField的快速基本使用代码块
    将UIImage转换成圆形图片image
    color转成image对象
  • 原文地址:https://www.cnblogs.com/WindSun/p/12543821.html
Copyright © 2020-2023  润新知