当前位置: 首页 > news >正文

linux protobuf的安装与使用

  首先,下载protobuf:

wget https://github.com/protocolbuffers/protobuf/releases/download/v21.11/protobuf-cpp-3.21.11.zip
 

然后,解压:

tar -xf protobuf-2.5.0.tar.gz

接着,安装protobuf

cd protobuf-3.21.11/
mkdir build
./configure --prefix=$PWD/build
make -j8
make check
make install

验证:

cd build/bin/
./protoc --version

使用:

1)定义.proto文件(contacts.proto)

syntax = "proto3";
package contacts;message PeopleInfo {string name = 1;int32 age = 2;
}

2)用protoc编译器编译.proto文件

protoc --cpp_out=. contacts.proto

3)使用生成的API

#include <iostream>
#include <fstream>
#include <string>
#include "contacts.pb.h"int main() 
{// Create a PeopleInfo messagecontacts::PeopleInfo person;person.set_name("John Doe");person.set_age(30);// Serialize to filestd::string filename = "person.bin";std::ofstream output(filename, std::ios::binary);if (!person.SerializeToOstream(&output)) {std::cerr << "Failed to write person." << std::endl;return -1;}output.close();// Read from file and deserializecontacts::PeopleInfo person2;std::ifstream input(filename, std::ios::binary);if (!person2.ParseFromIstream(&input)) {std::cerr << "Failed to read person." << std::endl;return -1;}// Verify the datastd::cout << "Name: " << person2.name() << std::endl;std::cout << "Age: " << person2.age() << std::endl;return 0;
}

4)编译

g++ main.cpp contacts.pb.cc -o main -L../lib/ -lprotobuf -I../include

5)运行

./main

觉得有帮助的话,打赏一下呗。。

           

需要商务合作(定制程序)的欢迎私信!! 

http://www.lryc.cn/news/498351.html

相关文章:

  • 关于Chrome自动同步书签的解决办法
  • 基于深度学习的甲状腺结节影像自动化诊断系统(PyQt5界面+数据集+训练代码)
  • docker常用操作命令
  • Android:生成Excel表格并保存到本地
  • JVM-程序计数器与栈
  • 【kotlin 】内联类(value class / inline class)
  • 【SpringBoot】使用IDEA创建SpringBoot项目
  • C++设计模式(原型、代理、适配器、组合)
  • 如何在CentOS 7上使用FreeIPA设置集中式Linux身份验证
  • vue2播放视频和预览文件的组件以及使用方法
  • 性能之巅:Go语言优化深度探索
  • react + antd desgin 使用form功能时upload,radio,checkbox不能回显的问题
  • 【08】MySQL复杂查询:子查询语句详解与示例
  • Unity 相机旋转及角度限制
  • error=‘null‘], commandType=io.lettuce.core.RedisPublisher$SubscriptionCommand]
  • Golang 字符串字面量表示方法
  • 03_Webpack模块打包工具
  • 【目标跟踪】AntiUAV600数据集详细介绍
  • 十、JavaScript的应用的习题
  • 【Spring】AOP
  • 三维地图,智慧城市,商业智能BI,数据可视化大屏(Cesiumjs/UE)
  • 鸿蒙Next通过oss上传照片到阿里云
  • 小白爬虫——selenium入门超详细教程
  • nlp培训重点
  • 什么是多模态和模态
  • apache中的Worker 和 Prefork 之间的区别是什么?
  • 系统监控——分布式链路追踪系统
  • 【Python]深入Python日志管理:从logging到分布式日志追踪的完整指南
  • DHCP Client的工作方式
  • docker-常用应用部署dockerfile模板