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 ...
随机推荐
- C语言求s=1+(1+2)+…+(1+2+3+…+n)的值,其中n由键盘输入。
#include<stdio.h> void main() { int n, s=0; scanf_s("%d", &n); for (n; n > 0; ...
- MySQL运行在docker容器中会损失多少性能
前言 自从使用docker以来,就经常听说MySQL数据库最好别运行在容器中,性能会损失很多.一些之前没使用过容器的同事,对数据库运行在容器中也是忌讳莫深,甚至只要数据库跑在容器中出现性能问题时,首先 ...
- 2020-2021 “Orz Panda” Cup Programming Contest G题(树形结构)
题目传送门 题目大意:给点一颗包含 \(n\)个节点的无根树,有 \(m\)次询问,每次询问给出两个点 \(u\)和 \(v\),要求计算 \[\sum_{r=1}^{n}d_{r}(u,v) \] ...
- jmeter工具中vars与props命令的区别和使用
话不多说直接干活!!!!! vars 和 props命令存在于"jsr223 预处理器"."BeanShell 预处理程序"."JSR223 后置处理 ...
- [ABC274F] Fishing
Problem Statement On a number line, there are $N$ fish swimming. Fish $i$, which has a weight of $W_ ...
- 5个免费、跨平台的SQLite数据库可视化工具
前言 SQLite是一个轻量级的嵌入式关系型数据库,目前最新的版本是 SQLite3.今天推荐5个实用的SQLite数据库可视化工具(GUI),帮助大家更好的管理SQLite数据库. 什么是SQLit ...
- nginx+lua+redis实现灰度发布
前言: 授人以鱼不如授人以渔.先学会用,在学原理,在学创造,可能一辈子用不到这种能力,但是不能不具备这种能力.这篇文章主要是沉淀使用nginx+lua+redis实现灰度,当我们具备了这种能力,随时可 ...
- 高斯朴素贝叶斯(Gaussian Naive Bayes)原理与实现——垃圾邮件识别实战
朴素贝叶斯(Naive Bayes): 根据贝叶斯定理和朴素假设提出的朴素贝叶斯模型. 贝叶斯定理: 朴素假设(特征条件独立性假设): 代入可知朴素贝叶斯模型计算公式: 因为朴素贝叶斯是用来分类任务, ...
- python tkinter使用(五)
python tkinter使用(五) 本篇文章讲述tkinter 中treeview的使用 Treeview是一个多列列表框,可以显示层次数据. #!/usr/bin/python3 # -*- c ...
- 【C#】【串口通信(Serial Port)】无法使用(using System.IO.Ports;)命名空间<Error:SerialPort不存在上下文>
1.包缺失导致--安装相应包: 2.等待命令行初始化--输入命令: Install-Package Microsoft.Windows.Compatibility -Version 5.0.2 参考网 ...