当前位置:网站首页>vs2019 中编译和使用 protobuf 库
vs2019 中编译和使用 protobuf 库
2022-07-27 00:18:00 【litanyuan】
背景
protobuf 是一种跨平台的序列化结构数据的方法,可用于网络数据传输及存储。
本文对使用 vs2019 编译 protobuf 库文件进行说明,相关代码及安装文件均来自官网下载。
VS2019 编译 protobuf
①.生成 sln 工程文件
打开 cmake ,然后选择 protobuf-v3.19.4 目录中的 cmake 文件夹

单击 configure 按钮,在打开界面中配置 生成器及平台

取消 protobuf_BUILD_TESTS 选项,若要生成动态库需要选择 protobuf_BUILD_SHARED_LIBS

单击 Generate 按钮生成 sln 工程目录

②.编译 sln 工程
用 VS2019 打开生成的 sln 项目文件
右键解决方案分别生成 Debug 和 Release 版的库文件

③.整理生成的结果
新建 bin 文件夹,把生成的 libprotobuf.dll、libprotobuf-lite.dll、libprotoc.dll 三个动态库文件放入文件夹

新建 lib 文件夹,把生成的 libprotobuf.lib、libprotobuf-lite.lib、libprotoc.lib三个库文件放入文件夹

新建 include 文件夹,把 protobuf-v3.19.4 目录中的 src\google 文件夹拷贝放入

新建 tools 文件夹,把 protoc.exe、libprotobuf.dll、libprotoc.dll 三个文件放入,用于编译生成 c++ 文件

最终生成的完整目录如下,在使用时把 protobuf-x86 放在对应的工程路径下即可

VS2019 使用 protobuf
①.项目属性配置
在附加包含目录中配置 include 路径

在附加库目录中配置 lib 路径

在附加依赖项中配置 libprotobufd.lib

②.编写测试用的 .proto 文件
syntax = "proto3";
package protoTest;
message DemoMsg
{
string name = 1;
int32 id = 2;
}
③.生成对应的 C++ 文件
为方便使用可在同目录编写一个 generate.bat 文件
cd /d %~dp0
..\protobuf-x86\tools\protoc.exe *.proto --cpp_out=.
pause
双击运行即可生成 .h 和 .cc 文件

把生成的文件添加到项目中,并且在生成的 .h 文件最开头添加 #define PROTOBUF_USE_DLLS,否则编译会出错

④.测试代码
#include <iostream>
#include "proto/protoDemo.pb.h"
int main()
{
protoTest::DemoMsg msg;
msg.set_id(1);
msg.set_name("hello");
std::cout << msg.SerializePartialAsString() << std::endl;
}


边栏推荐
- typora详细教程
- Goatgui invites you to attend a machine learning seminar
- idea中常用的快捷键
- Non global function of lua function
- 小程序怎样助力智能家居生态新模式
- If you want to thoroughly optimize the performance, you must first understand the underlying logic~
- 基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
- Scheduling of processes
- 聊聊连接池和线程
- 万字长文,带你搞懂 Kubernetes 网络模型
猜你喜欢

Plato Farm全新玩法,套利ePLATO稳获超高收益

智能指针shared_ptr、unique_ptr、weak_ptr

白盒测试案例设计(我爷爷都能看懂)

Goatgui invites you to attend a machine learning seminar

面试突击68:为什么 TCP 需要 3 次握手?

White box test case design (my grandfather can understand it)

Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion

手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践

无效的目标发行版:17 的解决办法

Jmeter接口测试, 快速完成一个单接口请求
随机推荐
小程序怎样助力智能家居生态新模式
CS224W fall 1.2 Applications of Graph ML
CuteOne:一款OneDrive多网盘挂载程序/带会员/同步等功能
Knowledge points of test questions related to software testing
Kubernetes Dashboard 部署应用以及访问
Rust web (I) -- self built TCP server
C语言程序的编译(预处理)下
As for the pit saved by serialized variables, the data added with indexer cannot be serialized
idea中常用的快捷键
Web3.0 world knowledge system sharing - what is Web3.0
小玩一个并行多线程MCU—MC3172
Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
Arduino UNO +74HC164流水灯示例
确定了,2022下半年软考报名8月开始
机器学习【Matplotlib】
红宝书第四版的一个错误?
Greed - 376. Swing sequence
Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion
CS224W fall 课程 ---- 1.1 why Graphs ?
Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失