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的源代码,在根目录./configure,make,make install安装完毕。进入到examples,make 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