在 mac 上编译协议缓冲区时出错

Error while compiling protocol buffer on mac

我正在按照协议缓冲区的教程进行操作,并且在编译时 运行 出现了不同的错误。我的 addressbook.proto 文件在 /Users/flexmaster411/protobuffer

protoc -I=/Users/flexmaster411/protobuffer --python_out= /Users/flexmaster411/protobuffer/addressbook.proto /Users/flexmaster411/protobuffer

即使我的原型文件中有语法 = "proto3",我仍然收到以下错误

[libprotobuf WARNING google/protobuf/compiler/parser.cc:471] No syntax specified for the proto file. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)

不确定我是否正确设置了导致此问题的目标文件夹。感谢任何帮助

syntax = "proto3";

package tutorial;

message Person {
  string name = 1;
  int32 id = 2;        // Unique ID number for this person.
  string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    string number = 1;
    PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;
}

// Our address book file is just one of these.
message AddressBook {
  repeated Person people = 1;
}

您似乎颠倒了参数的顺序。 /Users/flexmaster411/protobuffer 是您的输出目录,因此它应该与 --python_out 一起出现。由于您第二次指定它,protoc 认为您是在告诉它 /Users/flexmaster411/protobuffer 是一个 输入 。所以它试图打开一个 目录 然后将它解析为一个 .proto 文件。有趣的是,目录 returns 上的 read() 没有数据,protoc 将其解释为完全有效的 .proto 文件,只是没有声明任何内容!但是它随后会给你一个警告,因为这个空文件没有任何 syntax 行。

我想你想输入的是:

protoc -I=/Users/flexmaster411/protobuffer --python_out=/Users/flexmaster411/protobuffer /Users/flexmaster411/protobuffer/addressbook.proto