Windows10+VS2017+ProtocolBuffer3.17配置使用Demo详细

一、下载ProtocolBuffer、CMake

先去官网下载:ProtocolBuffer官网CMake官网
我这里用的是protobuf-cpp-3.17.3.zip、cmake-3.18.2-win64-x64

二、编译ProtocolBuffer

解压protobuf-cpp-3.17.3.zip,打开cmake-3.18.2-win64-x64\bin\cmake-gui.exe,选择目录;

在这里插入图片描述

选择对应的版本和平台;

在这里插入图片描述

点击Generate,构建vs项目

在这里插入图片描述

打开protobuf_32\protobuf.sln项目,选择解决方案,点击生成得到libprotobufd.lib、libprotocd.lib、protoc.exe文件;

在这里插入图片描述
在这里插入图片描述

点击protobuf_32\extract_includes.bat,生成include文件夹;

在这里插入图片描述

三、新建项目测试ProtocolBuffer

新建hello.proto文件,复制下边的代码保存

syntax = "proto3";
package tutorial;

message Hello{
  string say = 1;
}

当前hello.proto目录下打开cmd,执行protoc --cpp_out=./ hello.proto,得到hello.pb.h、hello.pb.cc文件;

vs新建控制台项目,添加include、lib文件夹,hello.pb.h、hello.pb.cc到项目中,vs中右键配置项目属性;
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

将下面代码拷贝到ProtobufTest32.cpp文件中保存,点击生成,运行;

#include <iostream>
#include <fstream>
#include <string>
#include "hello.pb.h"
using namespace std;

int main() {
	tutorial::Hello hi;
	hi.set_say("Hello World");

	// 序列化
	string str;
	hi.SerializeToString(&str);
	cout << "序列化后:" << str << endl;
	
	// 反序列化
	tutorial::Hello ha;
	if (!ha.ParseFromString(str))
	{
		cerr << "failed." << endl;
		return -1;
	}
	cout << "反序列化:" << ha.say() << endl;

	google::protobuf::ShutdownProtobufLibrary();
	return 0;
}

运行结果:

在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值