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
| `-- google
| `-- 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的安装/使用的更多相关文章

  1. google proto buffer安装和简单示例

    1.安装 下载google proto buff. 解压下载的包,并且阅读README.txt,根据里面的指引进行安装. $ ./configure $ make $ make check $ mak ...

  2. Google Protocol Buffer的安装与.proto文件的定义

    什么是protocol Buffer呢? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准. 我理解的就是:它是一种轻便高效的结构 ...

  3. Google Protocol Buffer的安装与.proto文件的定义(转)

    转自(https://www.cnblogs.com/yinheyi/p/6080244.html) 什么是protocol Buffer呢? Google Protocol Buffer( 简称 P ...

  4. google protocol buffer 简介 版本 安装 使用 实例

    一.简介 protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了三种语言的实现:java.c++ 和 python,每一种实现 ...

  5. Google Protocol Buffer安装编译及使用

    近期玩了玩谷歌的Protocol Buffer.以下就简介下 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准.眼下已经正在使用的 ...

  6. Google Protocol Buffer 的使用和原理[转]

    本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...

  7. Google Protocol Buffer 的使用和原理

    Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...

  8. Google Protocol Buffer

    Google Protocol Buffer(protobuf)是一种高效且格式可扩展的编码结构化数据的方法.和JSON不同,protobuf支持混合二进制数据,它还有先进的和可扩展的模式支持.pro ...

  9. 【Google Protocol Buffer】Google Protocol Buffer

    http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Google Protocol Buffer 的使用和原理 Protocol Buffers ...

随机推荐

  1. 【读书笔记】iOS-复制的种类

    一,你可以使用不同的方法复制对象.大多数对象都引用(即指向)其它对象. 二,浅层复制,不复制引用对象,新复制的对象只指向现有的引用对象.NSArray类的copy方法是浅层复制.当复制一个NSArra ...

  2. Mac下Apache Tomcat安装配置

    Java Web如果稍微知道一点,一般对Tomcat都不会陌生,Apache是普通服务器,本身只支持html即普通网页,可以通过插件支持PHP,还可以与Tomcat连通(单向Apache连接Tomca ...

  3. GCD编程 之 略微提高篇

    额外任务:学习YouXianMing封装好的GCD源码   1.GCD串行队列与并发队列   串行队列一次只执行一个线程,按照添加到队列的顺序依次执行 并发队列一次可以执行多个线程,线程的执行没有先后 ...

  4. Linux平台卸载MySQL总结【转】

    最近用到了mysql主从,顺手看到了这篇文章,拿出来分享一下. 转自:http://www.cnblogs.com/kerrycode/p/4364465.html 潇湘隐者 RPM包安装方式的MyS ...

  5. Cron 表达式详解和案例

    1. cron表达式格式: {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)} 2. cron表达式各占位符解释: {秒数} ==> 允许值范围: 0~59 ,不允许 ...

  6. du df 查看文件和文件夹大小

    http://www.cnblogs.com/benio/archive/2010/10/13/1849946.html du -h df -h du -h --max-depth=1 //  查看当 ...

  7. 连接Oracle远程数据库错误:ORA-12541,ORA-12514,ORA-01017的解决方法!

    1.出现如下错误:ORA-12541:TNS:no listener,如下图所示: 错误原因是我们没有开启Listener监听器服务,解决方法是在服务中开启这个服务,如下图所示. 2.出现如下错误:O ...

  8. Linux学习之四——磁盘与文件系统管理

    一.一些基本定义 1. superblock:记录此 filesystem 的整体信息,包括inode/block的总量.使用量.剩余量, 以及文件系统的格式与相关信息等:2. inode:记录档案的 ...

  9. 汇编中call printf参数压栈时错误理解

    EAX, ECX,EDX,EBX均可以32bit,16bit,8bit访问,如下所示: <-------------------EAX------------------------>|& ...

  10. 【Android 我的博客APP】1.抓取博客首页文章列表内容——网页数据抓取

    打算做个自己在博客园的博客APP,首先要能访问首页获取数据获取首页的文章列表,第一步抓取博客首页文章列表内容的功能已实现,在小米2S上的效果图如下: 思路是:通过编写的工具类访问网页,获取页面源代码, ...