Google proto buffer的安装/使用
protobuf安装/使用
原本是要在官网上下载的:
http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz
可惜已被墙,幸好有好心人提供了以下地址:
http://pan.baidu.com/s/1pJlZubT
为了说明安装过程中文件的作用,我就指定目录安装了:
./configure --prefix=/usr/local/protobuf/
make
make check
make install
当然,安装前需要确保自己安装了gcc,g++,automake等,就不多说了。
安装完成之后,大概生成了这样一个目录结构
|-- bin
| `-- protoc
|-- include
| `-- protobuf
| |-- compiler
| | |-- code_generator.h
| | |-- command_line_interface.h
| | |-- cpp
| | | `-- cpp_generator.h
| | |-- importer.h
| | |-- java
| | | `-- java_generator.h
| | |-- parser.h
| | |-- plugin.h
| | |-- plugin.pb.h
| | |-- plugin.proto
| | `-- python
| | `-- python_generator.h
| |-- descriptor.h
| |-- descriptor.pb.h
| |-- descriptor.proto
| |-- descriptor_database.h
| |-- dynamic_message.h
| |-- extension_set.h
| |-- generated_enum_reflection.h
| |-- generated_message_reflection.h
| |-- generated_message_util.h
| |-- io
| | |-- coded_stream.h
| | |-- gzip_stream.h
| | |-- printer.h
| | |-- tokenizer.h
| | |-- zero_copy_stream.h
| | |-- zero_copy_stream_impl.h
| | `-- zero_copy_stream_impl_lite.h
| |-- message.h
| |-- message_lite.h
| |-- reflection_ops.h
| |-- repeated_field.h
| |-- service.h
| |-- stubs
| | |-- atomicops.h
| | |-- atomicops_internals_arm_gcc.h
| | |-- atomicops_internals_arm_qnx.h
| | |-- atomicops_internals_atomicword_compat.h
| | |-- atomicops_internals_macosx.h
| | |-- atomicops_internals_mips_gcc.h
| | |-- atomicops_internals_pnacl.h
| | |-- atomicops_internals_x86_gcc.h
| | |-- atomicops_internals_x86_msvc.h
| | |-- common.h
| | |-- once.h
| | |-- platform_macros.h
| | |-- template_util.h
| | `-- type_traits.h
| |-- text_format.h
| |-- unknown_field_set.h
| |-- wire_format.h
| |-- wire_format_lite.h
| `-- wire_format_lite_inl.h
`-- lib
|-- libprotobuf-lite.a
|-- libprotobuf-lite.la
|-- libprotobuf-lite.so -> libprotobuf-lite.so.8.0.0
|-- libprotobuf-lite.so.8 -> libprotobuf-lite.so.8.0.0
|-- libprotobuf-lite.so.8.0.0
|-- libprotobuf.a
|-- libprotobuf.la
|-- libprotobuf.so -> libprotobuf.so.8.0.0
|-- libprotobuf.so.8 -> libprotobuf.so.8.0.0
|-- libprotobuf.so.8.0.0
|-- libprotoc.a
|-- libprotoc.la
|-- libprotoc.so -> libprotoc.so.8.0.0
|-- libprotoc.so.8 -> libprotoc.so.8.0.0
|-- libprotoc.so.8.0.0
`-- pkgconfig
|-- protobuf-lite.pc
`-- protobuf.pc
然后我们先贴代码:
lm.helloworld.proto
package lm;
message helloworld
{
required int32 id = ;
required string str = ;
optional int32 opt = ;
}
write.cc
#include<iostream>
#include<fstream>
#include<stdio.h>
#include "lm.helloworld.pb.h" using namespace std;
using namespace lm; int main()
{
lm::helloworld msg1;
msg1.set_id();
msg1.set_str("hello"); fstream output("./msg.pb", ios::out | ios::trunc | ios::binary); if(!msg1.SerializeToOstream(&output))
{
cerr << "Failed to write msg."<<endl;
return -;
}
return ;
}
read.cc
#include<iostream>
#include<fstream>
#include<stdio.h>
#include "lm.helloworld.pb.h" using namespace std;
using namespace lm; void listmsg(const lm::helloworld &msg)
{
cout<<msg.id()<<endl;
cout<<msg.str()<<endl;
} int main()
{
lm::helloworld msg1; fstream input("./msg.pb", ios::in | ios::binary); if(!msg1.ParseFromIstream(&input))
{
cerr << "Failed to write msg."<<endl;
return -;
}
listmsg(msg1);
return ;
}
首先需要生成一个类似于lm.helloworld.proto的接口类的接口文件
/usr/local/protobuf/bin/protoc -I ./ --cpp_out=./ lm.helloworld.proto
我们可以看见生成了如下两个文件,这是编译的时候需要的
lm.helloworld.pb.cc
lm.helloworld.pb.h
而/usr/local/protobuf/bin/protoc正是protobuff用来生成此文件的程序;
然后我们就可以编译了
g++ -I /usr/local/protobuf/include/ lm.helloworld.pb.cc read.cc -o read `pkg-config --cflags --libs /usr/local/protobuf/lib/pkgconfig/protobuf.pc`
g++ -I /usr/local/protobuf/include/ lm.helloworld.pb.cc write.cc -o write `pkg-config --cflags --libs /usr/local/protobuf/lib/pkgconfig/protobuf.pc`
分别生成的write和read的可执行程序,就是我们使用protobuff的例子,先执行./write,后执行./read,就会看见结果:
[root@MYCR Protobuf]# ./read hello
完
Google proto buffer的安装/使用的更多相关文章
- google proto buffer安装和简单示例
1.安装 下载google proto buff. 解压下载的包,并且阅读README.txt,根据里面的指引进行安装. $ ./configure $ make $ make check $ mak ...
- Google Protocol Buffer的安装与.proto文件的定义
什么是protocol Buffer呢? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准. 我理解的就是:它是一种轻便高效的结构 ...
- Google Protocol Buffer的安装与.proto文件的定义(转)
转自(https://www.cnblogs.com/yinheyi/p/6080244.html) 什么是protocol Buffer呢? Google Protocol Buffer( 简称 P ...
- google protocol buffer 简介 版本 安装 使用 实例
一.简介 protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了三种语言的实现:java.c++ 和 python,每一种实现 ...
- Google Protocol Buffer安装编译及使用
近期玩了玩谷歌的Protocol Buffer.以下就简介下 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准.眼下已经正在使用的 ...
- Google Protocol Buffer 的使用和原理[转]
本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...
- Google Protocol Buffer 的使用和原理
Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...
- Google Protocol Buffer
Google Protocol Buffer(protobuf)是一种高效且格式可扩展的编码结构化数据的方法.和JSON不同,protobuf支持混合二进制数据,它还有先进的和可扩展的模式支持.pro ...
- 【Google Protocol Buffer】Google Protocol Buffer
http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Google Protocol Buffer 的使用和原理 Protocol Buffers ...
随机推荐
- Unity3D事件函数的执行顺序 - 包含渲染等模块的完整版,中英文对照
原文地址: http://www.cnblogs.com/ysdyaoguai/p/3746828.html In Unity scripting, there are a number of eve ...
- C++中派生类对象的内存布局
主要从三个方面来讲: 1 单一继承 2 多重继承 3 虚拟继承 1 单一继承 (1)派生类完全拥有基类的内存布局,并保证其完整性. 派生类可以看作是完整的基类的Object再加上派生类自己的Objec ...
- Node.js(1)-helloworld
1.Node.Js 环境准备 下载地址: node.js http://nodejs.org/download/ vs.net集成开发环境: Node.js Tools for Visual St ...
- Oracle-创建服务器参数文件
允许使用传统的init.ora或SPFILE作为配置文件.但是建议所有数据库创建和使用一个SPFILE.可以从init.ora创建SPFILE SQL> CREATE spfile FROM p ...
- APP原型设计工具,哪家强?转自知乎
著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:李志超 链接:http://www.zhihu.com/question/20403141/answer/25329730 ...
- 爆零后的感受外加一道强联通分量HDU 4635的题解
今天又爆零了,又是又,怎么又是又,爆零爆多了,又也就经常挂嘴边了,看到这句话,你一定很想说一句””,弱菜被骂傻,也很正常啦. 如果你不开心,可以考虑往下看. 翻到E(HDU 4635 Strongly ...
- poj 2391 Ombrophobic Bovines(最大流+floyd+二分)
Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 De ...
- 构建多模块的Maven项目
在Eclipse下创建一个maven项目,该项目有多个模块组成. 1.创建父项目 File->New->Project->Maven->Maven Project(图一) ...
- [转载]在iTOP-4412开发板上调试helloworld应用
本文转自迅为论坛:http://www.topeetboard.com 1.安装ADB驱动 在开发板上调试 Android 应用,首先要安装 ADB 驱动. 通过“SDK Manager.exe”来安 ...
- IOS版本被拒的经历
IOS版本被拒的经历: 1,登陆方式依赖外部平台 因为我们的APP是只用微博登陆,想做成类似meerkat类型的,也能各种消息都同步微博. 结果当然行不通,这个确实是不听好人言,网上多个人都说过这个问 ...