protobuf使用简单示例

一.安装

首先下载protobuf的安装包,我这里使用的是protobuf-cpp-3.21.5.tar.gz

  1. 解压安装包

    tar -xzf protobuf-cpp-3.21.5.tar.gz
  2. 进入解压后的文件夹

    cd protobuf-3.21.5/
  3. 生成Makefile文件

    ./configure --prefix=/home/tdx/software/protobuf/protobuf-install
  4. 执行make编译

    make
    make check
  5. 安装

    make install

    ​ 可以看到在/home/tdx/software/protobuf/protobuf-install目录下有bin、include和lib目录。可以把include目录下的文件都按照该目录结构和lib/libprotobuf.a复制到所需要的目录中去,这样就可以开始写示例程序了。

二.编写示例程序

​ 创建一个protobuf_demo目录,将/home/tdx/software/protobuf/protobuf-install目录中的include和lib/libprotobuf.a拷贝到该目录下(注:lib目录下只保留libprotobuf.a静态库,不要留有动态库,否则后面链接会出现问题)。

  1. 编写Mymessage.proto代码
package Im;
message Content{
required int32 id = 1; //ID
required string str = 2; //str
optional int32 opt = 3; //optional field
}
  1. 编写Writer.cpp代码
#include <iostream>
#include <fstream>
#include "Mymessage.pb.h"
using namespace std;
int main(){
Im::Content msg1;
msg1.set_id(101);
msg1.set_str("zhangsan");
fstream output("./log", ios::out | ios::trunc | ios::binary);
if(!msg1.SerializeToOstream(&output)){
cerr << "Failed to write msg." << endl;
return -1;
}
return 0;
}
  1. 编写Reader.cpp代码
#include <iostream>
#include <fstream>
#include "Mymessage.pb.h"
using namespace std;
void ListMsg(const Im::Content &msg){
cout << msg.id() << endl;
cout << msg.str() << endl;
} int main(){
Im::Content msg1;
fstream input("./log", ios::in | ios::binary);
if(!msg1.ParseFromIstream(&input)){
cerr << "Failed to parse address book." << endl;
return -1;
}
ListMsg(msg1);
return 0;
}
  1. 编写Makefile文件
INC=/home/tdx/Desktop/study/protobuf_demo/include
LIB=/home/tdx/Desktop/study/protobuf_demo/lib lib=protobuf all:Writer Reader Writer.o:Writer.cpp
g++ -g -c Writer.cpp -I$(INC) -L$(LIB) -l$(lib) Reader.o:Reader.cpp
g++ -g -c Reader.cpp -I$(INC) -L$(LIB) -l$(lib) Writer:Writer.o Mymessage.pb.o
g++ -g -o Writer Writer.o Mymessage.pb.o -I$(INC) -L$(LIB) -l$(lib) Reader:Reader.o Mymessage.pb.o
g++ -g -o Reader Reader.o Mymessage.pb.o -I$(INC) -L$(LIB) -l$(lib) Mymessage.pb.o:Mymessage.pb.cc
g++ -g -c Mymessage.pb.cc -I$(INC) -L$(LIB) -l$(lib) clean:Writer Reader Writer.o Reader.o Mymessage.pb.o
rm -rf Writer Reader Writer.o Reader.o Mymeaasge.pb.o

​ 执行:

/home/tdx/software/protobuf/protobuf-install/bin/protoc -I=./ --cpp_out=./ Mymessage.proto

​ 此时会生成Mymessage.pb.h和Mymessage.pb.cc文件。再执行make命令,生成Writer和Reader文件。执行./Writer命令后,再执行./Reader命令,终端上输出:

101
zhangsan

参考:《后台开发 核心技术与实践》

​ 关于protobuf的详细应用查阅相关文档,未完待续......

protobuf安装、编译和使用的更多相关文章

  1. Linux下protobuf的编译与安装【各种奇葩问题】

    1.下载源码 首先,从github上下载protobuf的源码,地址:https://github.com/google/protobuf,我选择下载2.5.0版本. 2.编译protobuf 2.1 ...

  2. Linux下protobuf的编译与安装

    1.下载源码 首先,从github上下载protobuf的源码,地址:https://github.com/google/protobuf,我选择下载2.5.0版本. 2.编译protobuf 将下载 ...

  3. google protobuf安装与使用

    google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...

  4. 比特币Bitcoin源代码安装编译

    body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; }        比特币 (货币符号: ฿;英文名:Bitcoin;英文 ...

  5. linux centos安装编译phantomjs 2.0的方法

    phantomjs 2.0最新版的官方不提供编译好的文件下载,只能自己编译,有教程但是过于简单,特别是服务器上要安装N多的支持.折腾到现在终于装好了并且能正常运行了,截图mark一下: linux c ...

  6. linux_安装_安装编译phantomjs 2.0的方法_转

    项目中要对数据公式webkit渲染,phantmjs 2.0的效果好比1.9好不少. 安装过程中 坑比较多. 转载文章: phantomjs 2.0最新版的官方不提供编译好的文件下载,只能自己编译,有 ...

  7. wxWidgets的安装编译、相关配置、问题分析处理

    wxWidgets的安装编译.相关配置.问题分析处理 一.介绍部分 (win7 下的 GUI 效果图见 本篇文章的最后部分截图2张) wxWidgets是一个开源的跨平台的C++构架库(framewo ...

  8. FFmpeg在Linux下安装编译过程

    转载请把头部出处链接和尾部二维码一起转载,本文出自:http://blog.csdn.net/hejjunlin/article/details/52402759 今天介绍下FFmpeg在Linux下 ...

  9. openblas下载安装编译

    编译好的库: https://github.com/JuliaLinearAlgebra/OpenBLASBuilder/releases 源码编译 下载:https://github.com/xia ...

  10. Linux上安装编译工具链

    在Linux上安装编译工具链,安装它会依赖dpkg-dev,g++,libc6-dev,make等,所以安装之后这些依赖的工具也都会被安装.ubuntu软件库中这么描述 Informational l ...

随机推荐

  1. 节能减排 | AIRIOT智慧工厂节能管理解决方案

    工厂作为高能耗的生产型企业,降低能耗和提升资源利用率方面就显得很重要,对实施国家倡导的节能降耗.绿色发展有着很大程度上的必要性.然而,工厂能源管理从传统手段向智能化升级转型的过程中,企业也不可避免的面 ...

  2. 【算法】状态之美,TCP/IP状态转换探索

    最近城市里甲流肆虐,口罩已经成为了出门必备的物品.小悦也不得不开始采取防护措施,上下班过程中,将口罩戴起来以保护自己不受病毒的侵害. 每天下班后,小悦总是喜欢投入到自己的兴趣爱好中,她热衷于翻阅与IT ...

  3. Excel做数据分析?是真的很强!

    当涉及到数据分析时,Excel无疑是一个功能强大且广泛应用的工具.它提供了丰富的功能和灵活性,使得用户可以进行各种复杂的数据处理和分析.在本文中, 我将详细介绍Excel在数据分析领域的强大功能,包括 ...

  4. SpringCore完整学习教程3,入门级别

    从第三章开始 3. Profiles Spring profile提供了一种方法来隔离应用程序配置的各个部分,并使其仅在某些环境中可用.任何@Component.@Configuration或@Con ...

  5. springboot下添加日志模块和设置日志文件输出

    前言 日志的使用将通过SLF4J来使用,SLF4J(Simple Logging Facade for Java)是一个为Java应用提供简单日志记录的接口.它的主要目标是在不同的日志系统之间提供一个 ...

  6. [CF1830F] The Third Grace

    题目描述 You are given $ n $ intervals and $ m $ points on the number line. The $ i $ -th intervals cove ...

  7. shell中 << EOF 和 EOF 使用

    转载请注明出处: EOF(End of File)在Shell中通常用于指示输入的结束,并在脚本或命令中进行多行输入.它允许用户指定一个特定的分界符来表示输入的结束,通常用于创建临时文件.重定向输入或 ...

  8. 从零玩转设计模式之建造者模式-jianzaozhemoshi

    title: 从零玩转设计模式之建造者模式 date: 2022-12-08 18:15:30.898 updated: 2022-12-23 15:35:58.428 url: https://ww ...

  9. thymeleaf自定义标签

    前言 使用thymeleaf自定义标签,环境:springboot 2.3.7 + thymeleaf 3.0.11(2021-01-14最新版) 由于使用shiro,我们需要与thymeleaf整合 ...

  10. st_geometry、st_transform配置及问题汇总

    1.文件配置修改 路径上一定要使用双斜杠 2.使用sde账号登录,执行以下SQL,路径按照实际环境填写. select * from user_libraries; create or replace ...