如何包含具有相互依赖性的 .proto 文件

How to include .proto files having mutual dependency

我有两个 .proto 文件,其中有两个相互依赖的包。

a.proto

syntax = "proto3";
import "b.proto";

package a;

message cert {
    string filename = 1;
    uint32 length = 2;
}

enum state {
    UP = 1;
    DOWN = 2;
}

message events {
    repeated b.event curevent = 1;
    uint32 val = 2;
}

b.proto

syntax = "proto3";
import "a.proto";

package b;

message event {
     a.cert certificate = 1;
     a.state curstate = 2;
}

当我尝试生成 cpp 文件时,出现以下错误

# protoc -I. --cpp_out=. b.proto b.proto: File recursively imports itself: b.proto -> a.proto -> b.proto

如何实现?

注意:使用的协议版本是 libprotoc 3.3.0

proto 编译器不允许您包含循环依赖项。您将必须组织您的代码,以便没有任何递归导入。上述示例代码的一种组织方式可能是:

a.proto

syntax = "proto3";

package a;

message cert {
    string filename = 1;
    uint32 length = 2;
}

enum state {
    UNDEFINED = 0;
    UP = 1;
    DOWN = 2;
}

b.proto

syntax = "proto3";
import "a.proto";

package b;

message event {
    a.cert certificate = 1;
    a.state curstate = 2;
}

message events {
    repeated event curevent = 1;
    uint32 val = 2;
}

您的 events 类型没有使用 a.proto 中的任何内容,并且还使用 b.proto 中的 event 类型。将其移动到 b.proto.

是有意义的