使用指南可以看这篇文章介绍:
http://www.cppblog.com/liquidx/

下载Protocol Buffers:
http://code.google.com/p/protobuf/downloads/list

在vc环境下使用则在解压缩文件中有一个vsprojects文件夹, 使用vs来编译出libprotobuf.lib,libprotoc.lib
设置你的扩展头文件包含目录为 "D:\protobuf-2.1.0\src"

按照指南, 首先我们定义一个test.proto文件内容如下:

Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )package Test;
Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )
}



然后用protoc编译器编译出c++模块, 这里有一个已经编译好的编译器, 你也可以从压缩包中的源代码编译出该编译器.
http://protobuf.googlecode.com/files/protoc-2.1.0-win32.zip

用这个指令编译
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/test.proto
然后我们得到了
test.pb.h
test.pb.cc
2个c++文件

现在我们可以在项目中使用它了:

Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )#include <iostream>
Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )#include 
"test.pb.h"
Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )#include 
<fstream>
Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )
Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )#pragma comment( lib, 
"libprotobuf.lib" ) 
Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )#pragma comment( lib, 
"libprotoc.lib" ) 
Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )
Google Protocol Buffers( 比xml快20~100倍, 支持序列化数据 )
int _tmain(int argc, _TCHAR* argv[])
}



输出:
ID : 123
name : liquidx
e-mail : liquidx@163.com

产生的person.pb内容如下(28字节):
liquidx{liquidx@163.com

试用完毕:
感觉Protocol Buffers挺好用的, 项目的某些xml部分可以使用它来替代,这样在数据读取和操作上比xml更加方便直接, 且效率高效!
用它也可以在网络处理上得到一些好处!

 

相关参考:
http://www.cppblog.com/liquidx/archive/2009/06/23/88377.html

相关文章:

  • 2022-12-23
  • 2021-10-28
  • 2021-12-26
  • 2021-11-28
  • 2021-09-29
  • 2021-09-27
  • 2022-02-05
猜你喜欢
  • 2021-05-15
  • 2021-11-06
  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-11-19
相关资源
相似解决方案