google protobuf 学习

1、    网络资源: http://code.google.com/p/protobuf

 

C++的例子: http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/cpptutorial.html

 

 

 

2、    实践

从网上http://code.google.com/p/protobuf/downloads/list下载Protocol Buffers的源代码,在根目录./configuremakemake install安装完毕。进入到examplesmake cpp,仅编辑c++代码。运行如下:

 

[chengyi@localhost examples]$ ./add_person_cpp cy_test.txt

cy_test.txt: File not found.  Creating a new file.

Enter person ID number: 1

Enter name: cheng yi

Enter email address (blank for none): 北京市朝阳区

Enter a phone number (or leave blank to finish): 13800000000

Is this a mobile, home, or work phone? mobile

Enter a phone number (or leave blank to finish):

 

[chengyi@localhost examples]$ ./list_people_cpp cy_test.txt

Person ID: 1

  Name: cheng yi

  E-mail address: 北京市朝阳区

  Mobile phone #: 13800000000

 

其中 cy_test.txt 内容如下:

 

   

1

^Hcheng yi^P^A^Z^R北京市朝阳区"^O

^K13800000000^P^@

 

可以看到,已经被编码过。

 

 

除了关心cpp代码之外,还有一个proto文件需要注意:

 

       /* addressbook.proto */

package tutorial;

 

option java_package = "com.example.tutorial";

option java_outer_classname = "AddressBookProtos";

 

message Person {

  required string name = 1;

  required int32 id = 2;        // Unique ID number for this person.

  optional string email = 3;

 

  enum PhoneType {

    MOBILE = 0;

    HOME = 1;

    WORK = 2;

  }

 

  message PhoneNumber {

    required string number = 1;

    optional PhoneType type = 2 [default = HOME];

  }

 

  repeated PhoneNumber phone = 4;

}

 

// Our address book file is just one of these.

message AddressBook {

  repeated Person person = 1;

}

 

 

3、封装:将C++接口封装成C函数

首先进行了一些尝试: http://blog.csdn.net/flykobesummer/archive/2009/08/10/4429831.aspx

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值