1、 Required fields are not allowed in proto3
原因:proto2和proto3的语法不同,proto3”仅仅支持repeated字段修饰,如果使用不支required,optional。针对不同版本要对应不同语法:
proto2语法:
syntax = "proto2";
message Person
{
required string name = 1;
required int32 id = 2;
required string email = 3;}
proto3语法:
syntax = "proto3";
message Person
{
string name = 1;
}
2、No syntax specified for the proto file: addrBook.proto. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
在proto文件最开头加上:
syntax = "proto2"; 或者 syntax = "proto3";
即可。
3、Explicit default values are not allowed in proto3.
因为在message里面定义元素时:
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
proto3不支持: [default = HOME]
改为
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
即可。
4、undefined reference to `google::protobuf::internal::VerifyVersion(int, int, char const*)'
编译时添加:`pkg-config --cflags --libs protobuf` 如下:
g++ writeMsg.cpp addrBook.pb.cc -o writeMsg `pkg-config --cflags --libs protobuf`
即可编译成功