centos安装protobuf并测试运行

clash安装

arch
如果是aarch64那么下载arm64位的
如果是x86_64那么下载amd64位的
链接 https://github.com/Dreamacro/clash/releases

 wget https://github.com/Dreamacro/clash/releases/download/v1.14.0/clash-linux-amd64-v1.14.0.gz

centos安装

yum -y install autoconf automake libtool

yum -y install gcc gcc-c++

2.解压, tar zxvf protobuf-all-3.5.1.tar.gz.进入解压目录 cd protobuf-3.5.1/

3.安装依赖:sudo yum install autoconf  automake libtool

4.生成编译配置文件:./autogen.sh

5.运行配置脚本,指定编译器,./configure

6.编译:make

7.安装:make install

8.更新库:sudo ldconfig

9.测试:protoc --version


vim /etc/ld.so.conf.d/libprotobuf.conf  #添加下面内容
/usr/local/lib
#然后执行下面命令
sudo ldconfig
vim /etc/profile # 修改文件 添加下面内容
LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
#执行下面命令
source /etc/profile

新建test.proto文件

syntax = "proto3";

package fixbug;

//定义登录消息类型 name pwd
message LoginRequest
{
    string name = 1;
    string pwd = 2;
}

message LoginResponse
{
    int32 errcode = 1;
    string errmsg = 2;
    bool success = 3;

}

执行protoc test.proto --cpp_out=./
然后会多出几个文件来
新建main.cc文件

#include "test.pb.h"
#include <iostream>
#include <string>
using namespace fixbug;
using namespace std;


int main(){
    //封装了login请求对象的数据
    LoginRequest req;
    req.set_name("zhang san");
    req.set_pwd("123456");

    //对象数据序列化 =》char*
    std::string send_str;
    req.SerializeToString(&send_str);
    {
        std::cout << send_str.c_str() << std::endl;
    }
    //从send_str反序列化一个login请求对象
    LoginRequest reqB;
    if(reqB.ParseFromString(send_str))
    {
        std::cout << reqB.name() << std::endl;
        std::cout << reqB.pwd() << std::endl;
    }
    return 0;
}

执行编译

g++ main.cc test.pb.cc -lprotobuf

尝试运行 ./a.out

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贪睡的蜗牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值