步骤:
- 下载protobuf-2.6.1.zip和protoc-2.6.1-win32.zip,地址:https://github.com/google/protobuf/tags
- 到目录protobuf-2.6.1\vsprojects下打开protobuf.sln
- 将项目libprotobuf设置为启动项目
- 运行
错误:
<hash_map> is deprecated and will be REMOVED
项目属性->C++->预处理器->预处理器定义:_SILENCE_STDEXT_HASH_DEPRECATION_WARNING
简单介绍:
通常,编写一个protocol buffers应用需要经历如下三步:
- 定义消息格式文件,最好以proto作为后缀名
- 使用Google提供的protocol buffers编译器来生成代码文件,一般为.h和.cc文件,主要是对消息格式以特定的语言方式描述
proto文件例子:
message Person {
required string name = 1;
required int32 id = 2;
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;
required bytes unsure = 5; //Add byte array here
}
message AddressBook {
repeated Person = 1;
}
编译:protoc.exe -I=./ –cpp_out=./ people.proto
-I代表people.proto目录,–cpp_out代表输出目录,people.proto代表要编译的文件