protobuf安装、编译和使用
protobuf使用简单示例
一.安装
首先下载protobuf的安装包,我这里使用的是protobuf-cpp-3.21.5.tar.gz
解压安装包
tar -xzf protobuf-cpp-3.21.5.tar.gz
进入解压后的文件夹
cd protobuf-3.21.5/
生成Makefile文件
./configure --prefix=/home/tdx/software/protobuf/protobuf-install
执行make编译
make
make check
安装
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静态库,不要留有动态库,否则后面链接会出现问题)。

- 编写Mymessage.proto代码
package Im;
message Content{
required int32 id = 1; //ID
required string str = 2; //str
optional int32 opt = 3; //optional field
}
- 编写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;
}
- 编写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;
}
- 编写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安装、编译和使用的更多相关文章
- Linux下protobuf的编译与安装【各种奇葩问题】
1.下载源码 首先,从github上下载protobuf的源码,地址:https://github.com/google/protobuf,我选择下载2.5.0版本. 2.编译protobuf 2.1 ...
- Linux下protobuf的编译与安装
1.下载源码 首先,从github上下载protobuf的源码,地址:https://github.com/google/protobuf,我选择下载2.5.0版本. 2.编译protobuf 将下载 ...
- google protobuf安装与使用
google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...
- 比特币Bitcoin源代码安装编译
body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; } 比特币 (货币符号: ฿;英文名:Bitcoin;英文 ...
- linux centos安装编译phantomjs 2.0的方法
phantomjs 2.0最新版的官方不提供编译好的文件下载,只能自己编译,有教程但是过于简单,特别是服务器上要安装N多的支持.折腾到现在终于装好了并且能正常运行了,截图mark一下: linux c ...
- linux_安装_安装编译phantomjs 2.0的方法_转
项目中要对数据公式webkit渲染,phantmjs 2.0的效果好比1.9好不少. 安装过程中 坑比较多. 转载文章: phantomjs 2.0最新版的官方不提供编译好的文件下载,只能自己编译,有 ...
- wxWidgets的安装编译、相关配置、问题分析处理
wxWidgets的安装编译.相关配置.问题分析处理 一.介绍部分 (win7 下的 GUI 效果图见 本篇文章的最后部分截图2张) wxWidgets是一个开源的跨平台的C++构架库(framewo ...
- FFmpeg在Linux下安装编译过程
转载请把头部出处链接和尾部二维码一起转载,本文出自:http://blog.csdn.net/hejjunlin/article/details/52402759 今天介绍下FFmpeg在Linux下 ...
- openblas下载安装编译
编译好的库: https://github.com/JuliaLinearAlgebra/OpenBLASBuilder/releases 源码编译 下载:https://github.com/xia ...
- Linux上安装编译工具链
在Linux上安装编译工具链,安装它会依赖dpkg-dev,g++,libc6-dev,make等,所以安装之后这些依赖的工具也都会被安装.ubuntu软件库中这么描述 Informational l ...
随机推荐
- 使用QPainter制作一个简易的相册
PlayImage 记得一键三连哦 一个使用简单的QPainter绘图事件实现图片播放器的简易demo 支持图片切换 支持多路更新,自己扩展即可 支持幻灯片播放 PlayImage自定义控件支持复用, ...
- json数组根据某属性去重
数据: let arry = [ {name: "张三", age: 23, work: '计算机'}, {name: "王五", age: 29, work: ...
- K8s 多租户方案的挑战与价值
在当今企业环境中,随着业务的快速增长和多样化,服务器和云资源的管理会越来越让人头疼.K8s 虽然很强大,但在处理多个部门或团队的业务部署需求时,如果缺乏有效的多租户支持,在效率和资源管理方面都会不尽如 ...
- 环形缓冲区 Ring Buffer 的实现
环形缓冲区(Circular Buffer 或 Ring Buffer)是一种数据结构,它在逻辑上形成一个闭环.这种结构非常适用于需要固定大小的缓冲区的情况,如音频处理.网络通信.实时数据传输等.环形 ...
- Istio 网格的出口定义者:深入了解 Egress Gateway
本文分享自华为云社区<Istio Egress 出口网关使用>,作者:k8s技术圈. 前面我们了解了位于服务网格内部的应用应如何访问网格外部的 HTTP 和 HTTPS 服务,知道如何通过 ...
- [ABC279G] At Most 2 Colors
Problem Statement There is a grid with $1 \times N$ squares, numbered $1,2,\dots,N$ from left to rig ...
- docker 设计及源码分析
1.dockerd 是一个长期运行的守护进程(docker daemon).负责管理 docker 容器的生命周期.镜像和存储等.实际还是通过grpc 的协议调用 containerd 的 api 接 ...
- HDU-3591 混合背包
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- idea2020下载、安装、破解、配置
idea2020下载.安装.破解.配置 idea2020下载 [推荐]官方下载地址:https://www.jetbrains.com/idea/download/other.html 进入后往下找, ...
- MinIO客户端之rm
MinIO提供了一个命令行程序mc用于协助用户完成日常的维护.管理类工作. 官方资料 mc rm 删除指定的对象. 准备待删除的对象,查看对象,命令如下: ./mc ls local1/bkt2/ 控 ...