安装protobuf(cpp版)

12 篇文章 0 订阅

下载

注:访问外网会慢,用迅雷下载再传到你的服务器也是不错的方法

wget https://github.com/protocolbuffers/protobuf/releases/download/v21.8/protobuf-cpp-3.21.8.zip

安装

unzip protobuf-cpp-3.21.8.zip
cd protobuf-3.21.8
./configure --prefix=/usr
make
make check
sudo make install
sudo ldconfig

检查

有打印目录就是安装成功了

whereis protoc
which protoc

测试

test.proto文件

syntax = "proto2";
package common;

message MsgPerson
{
    required uint32 ID = 1;
    required uint32 age = 2;
    required string name = 3;
    required uint32 sex = 4;
}

将proto文件编译成 cpp文件

protoc -I=./ --cpp_out=./ ./test.proto
## 生成 test.pb.h, test.pb.cc

cpp代码:
test.cpp

#include<iostream>
#include "test.pb.h"
using namespace std;
int main()
{
	common::MsgPerson buffInfo;
	buffInfo.set_id(1);
	buffInfo.set_age(27);
	buffInfo.set_sex(1);
	buffInfo.set_name("1024");
	cout<<"++++++++++"<<endl;
	cout<<"id:"<< buffInfo.id()<<endl;
	cout<<"age:"<< buffInfo.age()<<endl;
	cout<<"sex:"<< buffInfo.sex()<<endl;
	cout<<"name:"<< buffInfo.name()<<endl;
	
	return 0;
}

编译代码

g++ -o testproto ./test.cpp ./test.pb.cc -lprotobuf  ## -lprotobuf必须要加,不然会报错,调用的函数未定义
./testproto 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值