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. android学习笔记 activity生命周期&任务栈&activity启动模式

    activity生命周期 完整生命周期 oncreate->onstart->onresume->onpause->onstop->ondestory 使用场景:应用程序 ...

  2. tomcat部署

    因为之前一直用的JBoss服务器,今天测试了一下想换成tomcat,然后就考了一个现成的tomcat,然后将jboss下的项目复制了一个到tomcat下,然后改了一下文件名,结果启动时报错 java. ...

  3. [PL/SQL工具]绿色版PLSQL工具登录时提示初始化失败,无法锁定OCI.dll错误

    问题现象:使用绿色版PL/SQL工具进行登录时报如下截图错误: 问题描述:初始化失败,无法锁定oci.dll 解决方法:在PLSQL的菜单栏里依次选择 工具->首选项,在OCI库(自动检测为空) ...

  4. C++基础——子类转父类转子类 (派生类转基类转派生类)

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  5. Windows Log4日志发送到ElasticSearch

    处理多行数据到elasticsearch Nxlog 配置 <Input in> Module im_file File "E:\\log\\webapi\\\err.log&q ...

  6. oracle创建数据库和用户

    以前开发的时候用得比较多的是mysql和sql server,oracle用的比较少,用起来比较生疏,mysql和sql server用起来比较类似,就oracle的使用方式和他们不同,oracle在 ...

  7. 获取某地的经纬度 && 通过经纬度获取相应的地理位置

    最近要通过一个经纬度判断该经纬度是否位于某个地区内,所以通过网上查找资料,整合后出了下面的内容. 1.通过地址获取改地址的经纬度 /** * @param addr * 查询的地址 * @return ...

  8. Linux gcc命令

    一.简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objectiv ...

  9. WCF 删除队列

    Configuration config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None); Serv ...

  10. 为TFS配置跨平台的生成服务器Xplat (Ubuntu Linux)

    1. 概述 从TFS 2015开始,微软开始支持跨平台的构建代理.你可以使用TFS的Xplat代理,方便的在基于IOS, Unix和Linux的服务器上搭建生成代理,实现构建.发布等功能.本文档已Ub ...