【问题标题】:How do I properly specify the location of my proto files in protocol buffers?如何正确指定我的 proto 文件在协议缓冲区中的位置?
【发布时间】:2020-11-28 17:35:11
【问题描述】:

这是我正在使用的三个文件:

// city.proto
syntax = "proto3";
package city;

message City {

    string cityName = 1;
    string zipCode = 2;
    string countryName = 3;

}

// street.proto
syntax = "proto3";

import "Exercise/city.proto";
package street;

message Street {

    string cityName = 1;
    city.City city = 2;

}

// building.proto
syntax = "proto3";

import "Exercise/street.proto";
package building;

message Building {

    string buildingName = 1;
    string buildingNumber = 2;
    street.Street street = 3;

}

这是我当前的目录结构:

   - PROTOCOLBUFFERS (folder on desktop)
        - Exercise
           - city.proto
           - street.proto
           - building.proto

这是我用来从 proto 文件生成代码的命令 protoc -I="."/Exercise --java_out=Exercise Exercise/*.proto

我正在使用我的终端在 PROTOCOLBUFFERS 文件夹中运行此命令。

我在执行此命令时做错了什么?我在窗户上。 这是我收到的错误消息,在线搜索它没有用。

building.proto:3:1:未找到导入“Exercise/street.proto”或出现错误。
building.proto:10:5: "street.Street" 未定义。

【问题讨论】:

    标签: protocol-buffers protoc


    【解决方案1】:

    我不熟悉在 Windows 上运行 protoc,但是...

    依次尝试以下各项:

    1. 将 Linux 路径分隔符 / 替换为 Windows 分隔符 \
    2. 使用来自驱动器根目录的绝对路径:protoc --proto_path=c:\...\Exercise --java_out=Exercise c:\...\Exercise\*.proto,即将c:\... 替换为正确的路径。
    3. 如果这不起作用,请将单个通配符 (*.proto) 替换为每个原型 c:\...\Exercise\city.proto c:\...\Exercise\building.proto c:\...\Exercise\street.proto 的完整路径

    protoc 是“挑剔的”。如果您需要使用proto_path,则应在后续对 proto 文件的引用中重复相应的路径。

    我很惊讶地看到文档表明 Java 不支持“导入”!?我使用 Java 以外的语言,如果这是真的,我会感到惊讶,但这就是它所说的:

    https://developers.google.com/protocol-buffers/docs/proto3#importing_definitions

    【讨论】:

      【解决方案2】:

      您应该更改原始文件中的导入路径:它们已经在同一个文件夹中。所以改变为例:

      // street.proto
      syntax = "proto3";
      
      import "city.proto";
      

      而不是

      // street.proto
      syntax = "proto3";
      
      import "Exercise/city.proto";
      

      此修复后,该命令将文件生成为:

      .
      ├── Exercise
      │   ├── building
      │   │   └── BuildingOuterClass.java
      │   ├── building.proto
      │   ├── city
      │   │   └── CityOuterClass.java
      │   ├── city.proto
      │   ├── street
      │   │   └── StreetOuterClass.java
      │   └── street.proto
      └── README.md
      

      希望有帮助

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2017-05-02
        • 1970-01-01
        • 1970-01-01
        • 2018-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多