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. C/C++ 通过SQLiteSDK增删改查

    SQLite,作为一款嵌入式关系型数据库管理系统,一直以其轻量级.零配置以及跨平台等特性而备受青睐.不同于传统的数据库系统,SQLite是一个库,直接与应用程序一同编译和链接,无需单独的数据库服务器进 ...

  2. 配置postcss-pxtorem报:options has an unknown property 'plugins'

    闲聊: 小颖最近在坐大屏相关的项目,要写适配,之前用的:postcss-px2rem.px2rem-loader,和朋友闲聊呢他说他们也在写大屏,不过他们用的 postcss-pxtorem,在写另外 ...

  3. 京东面试:说说Cookie、Session和Token的区别?

    东子作为目前传统电商三巨头之一(其他还有阿里巴巴和拼多多),其面试题的难度也中规中矩,总体来说没有其他两家面试难度高,当然薪资也没有其他两家薪资高. 其中拼多多的薪资最为离谱,尤其是前几年,听说挖同行 ...

  4. 如何实现图像搜索,文搜图,图搜图,CLIP+faiss向量数据库实现图像高效搜索

    如何实现图像搜索,文搜图,图搜图,CLIP+faiss向量数据库实现图像高效搜索 这是AIGC的时代,各种GPT大模型生成文本,还有多模态图文并茂大模型, 以及stable diffusion和sta ...

  5. 江西财经大学第一届程序设计竞赛 G题小Q的口袋校园

    题目链接:https://www.nowcoder.com/acm/contest/115/G 解题思路:题解就一份代码,贪心的思想.先按开始时间进行排序. 然后不断贪心获得happy[ j ]的最大 ...

  6. 安装华企盾DSC防泄密软件造成CAD2012卡住怎么办?

    将下图目录的.exe程序删除或者重命名

  7. python 获取android 应用使用情况

    python 获取android 应用使用情况 本文主要讲述python 脚本获取android 应用使用情况. 主要思路:使用adb 获取当前activity ,1s 一次输出. 主要涉及知识点: ...

  8. JavaScript forEach 方法跳出循环

    for循环 JavaScript中,for循环可以使用 break 和 continue 来跳出: continue:跳出本次循环 break:结束循环 for (let i = 0; i < ...

  9. Ef Core花里胡哨系列(7) 使用Ef Core也能维护表架构?

    Ef Core花里胡哨系列(7) 使用Ef Core也能维护表架构? 我们这里指的并不是查询,而是利用Ef的迁移原理,生成可用的其它表架构操作的Sql. 例如你想在Ef Core里建表,并且可能程序里 ...

  10. Python——第一章:循环语句while

    循环语句可以让我们的代码重复的去执行 while循环: while 条件:        代码 过程: 判断while循环的条件是否为真, 如果真, 执行代码. 然后再次判断条件.....直到条件为假 ...