google protocol buffer (C++,Java序列化使用实例)
转载,请注明出处: http://blog.csdn.net/eclipser1987/article/details/8525383 (eclipser@163.com)
1.下载安装:
google protocol buffer 的官网地址是:http://code.google.com/p/protobuf/
建议下载稳定版本:protobuf-2.4.1 linux下载protobuf-2.4.1.tar.bz2 windows下载protobuf-2.4.1.zip
这里以linux下安装为实例:
tar -xvf protobuf-2.4.1.tar.bz2
cd protobuf-2.4.1
./configure --prefix=/usr/local/protobuf-2.4.1
make
make install
2.使用protobuf
查看编译生成的目录
cd /usr/local/protobuf-2.4.1
ls
bin include lib
其中,bin中的protoc是.proto文件的处理器,可用这个工具生成cpp,java,python文件.
由于系统常用这个工具,可以将其ln或者直接拷贝到系统环境bin下
ln -s /usr/local/protobuf-2.4.1/bin/protoc /usr/bin/protoc
同样,可以将头文件ln或者直接拷贝到系统环境
ln -s /usr/local/protobuf-2.4.1/include/google /usr/include/google
将lib文件ln或者直接拷贝到系统环境
略,方法同上.
这个时候,protobuf的开发环境已经搭建了.
3.如何使用protobuf
package cn.vicky.model.seri;message User { required int32 id = 1; // 主键,唯一 required string username = 2; // 帐号 required string password = 3; // 密码 optional string email = 4; // 邮箱(可选) message Person { required int32 id = 1; // 主键,唯一 required string name = 2; // 角色名字 // 枚举类型 enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 3; // 电话号码(可以重复) } repeated Person person = 5; // 账户拥有的角色(可以重复)}