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
| `-- 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 = 1;
        required string str = 2;
        optional int32 opt = 3;
}

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(101);
        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 -1;
        }
        return 0;
}

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 -1;
        }
        listmsg(msg1);
        return 0;
}

首先需要生成一个类似于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,就会看见结果:

[[email protected] Protobuf]# ./read
101
hello

时间: 2024-08-05 06:40:49

Google proto buffer的安装/使用的相关文章

google proto buffer安装和简单示例

google proto buffer安装和简单示例 1.安装 下载google proto buff. 解压下载的包,并且阅读README.txt,根据里面的指引进行安装. $ ./configure $ make $ make check $ make install 没有意外的话,前面三步应该都能顺利完成,第四步的时候,需要root权限.我采用的默认的路径,所以,仅仅用root权限,还是安装不了,要自己先在/usr/local下新建一个lib的目录,然后执行make install,这样,

eclipse4.4的google protocol buffer的proto文件编辑器Protocol Buffer Editor安装

eclipse4.4的proto文件编辑器Protocol Buffer Editor安装 google protocol buffer 文件编辑器. 插件项目名称为protobuf-dt,是托管在google code上,国内现在已经连接不上google code了,在eclipse市场可以发现该插件,但是不能安装.在github上发现 有人已经做了相关的同步,可以在下面地址下载. 点击打开链接 其他地址也可以. 本人的为eclipse-jee-luna-SR2-win32-x86_64,安装

Google Protocol Buffer安装编译及使用

最近玩了玩谷歌的Protocol Buffer,下面就简单介绍下 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过12,183 个.proto 文件,他们用于RPC 系统和持续数据存储系统.Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储

【Google Protocol Buffer】Google Protocol Buffer

http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩展的序列化结构数据格式.目前提供了 C++.Java.Python 三种语言的 API. 17 评论: 刘 明, 软件工程师, 上海交大电

Google protocol buffer 使用和原理浅析 - 附带进阶使用方式

Protocol Buffer ??Google Protocol Buffer又简称Protobuf,它是一种很高效的结构化数据存储格式,一般用于结构化数据的串行化,简单说就是我们常说的数据序列化.这种序列化的协议非常轻便高效,而且是跨平台的,目前已支持多种主流语言(3.0版本支持C++, JAVA, C#, OC, GO, PYTHON等). ??通过这种方式序列化得到的二进制流数据比传统的XML, JSON等方式的结果都占用更小的空间,并且其解析效率也更高,用于通讯协议或数据存储领域是非常

转Google Protocol Buffer 的使用和原理

Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩展的序列化结构数据格式.目前提供了 C++.Java.Python 三种语言的 API. 2010 年 11 月 18 日 内容 简介 一个简单的例子 和其他类似技术的比较 高级应用话题 Protobuf 的更多细节 结束语 参考资料 评论 在

在 go/golang语言中使用 google Protocol Buffer

怎么在go语言中实用google protocol Buffer呢? 现在的潮流趋势就是一键搞定,跟ubuntu安装软件一样 go get code.google.com/p/goprotobuf/{proto,protoc-gen-go} go install  code.google.com/p/goprotobuf/proto 搞定,可以在 $GO_PATH/bin下找到 protoc-gen-go 这个程序,那么就可以实用protoc-gen-go 进行go语言的proto文件的自动生成

golang环境protocol buffer的安装

怎么在go语言中使用google protocol Buffer呢? 1.下载相应的proto版本:https://github.com/google/protobuf/releases 2.把bin下的protoc文件 copy到GOPATH目录和/usr/local/bin目录里 3.下载protoc-gen-go插件:https://github.com/golang/protobuf/ 4.把上述文件拷贝到GOPATH路径下 5.cd到protobuf下执行make 6.到GOPATH目

Google protocol buffer使用笔记

一 下载 Google下载地址:https://developers.google.com/protocol-buffers/docs/downloads?hl=zh-CN Github下载地址:https://github.com/google/protobuf 我这里下载版本:protobuf-2.6.1.tar.gz 二 编译 1 解压 将上面的压缩包解压到文件夹 protobuf-2.6.1 中. 2 编译 2.1 在protobuf-2.6.1中,找到vsprojects/protob