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 ...
随机推荐
- 升级到 Pulsar3.0 后深入了解 JWT 鉴权
背景 最近在测试将 Pulsar 2.11.2 升级到 3.0.1的过程中碰到一个鉴权问题,正好借着这个问题充分了解下 Pulsar 的鉴权机制是如何运转的. Pulsar 支持 Namespace/ ...
- python之range()、arange()和linspace()
目录 range() arange() linspace() range() range()格式如:range(start, stop, step) start:开始的数值,默认从0开始 stop:结 ...
- 【VMware vCenter】使用cmsso-util命令进行链接、删除、修改多个vCenter Server(VCSA)的SSO域。
VMware vCenter Server 支持新安装的时候选择将vCenter SSO域加入到另外一个现有的SSO域中,同时也支持使用cmsso-util命令将现有的两个或多个vCenter SSO ...
- 海量电商数据与用友YS系统数据对接案例
案例背景 客户是历史比较悠久的企业.企业内部用的系统多达十几套,专门成立信息化公司进行数字化转型,第一期需求系统旺店通的ERP以及旺店通的WMS并且启用京东的沧海外仓. 在选型ERP用友ERP和金蝶E ...
- JeecgBoot 框架升级至 Spring Boot3 的实战步骤
JeecgBoot 框架升级 Spring Boot 3.1.5 步骤 JEECG官方推出SpringBoot3分支: https://github.com/jeecgboot/jeecg-boot/ ...
- Codeforces Round #426 (Div. 2) A. The Useless Toy
A. The Useless Toy time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 【C#】【System.Linq】一些便捷的数据处理方法(Range、Select)
因为用习惯了Python中一些便捷的方法,随即查询C#中有没有类似的. 一.Range()方法 在Python中,range(Start,End,Step)可以直接生成一个可迭代对象,便用于需要循环多 ...
- NC65单据模板公式使用
单据模板公式使用 (一) 公式使用场景 用户使用产品时,往往对单据上的字段取值有各种不同的需求.为此单据模板提供 了模板公式功能,可以让实施顾问或者用户通过配置各种公式,并且不用修改代码,从 而满足用 ...
- Docker安装 配置
Docker的技术原理: 1. Linux 命名空间(Namespaces) 进程命名空间:使得每个容器拥有独立的进程空间,互相隔离,不受其他容器影响. 网络命名空间:提供独立的网络栈,每个容器有自己 ...
- SQLServer清空数据库日志
-------SQl2008以上 清空日志-------- USE[master] GO ALTER DATABASE [数据库名称] SET RECOVERY SIMPLE WITH NO_WAIT ...