2

我有两个 .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

4

1 回答 1

1

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类型不使用任何 from a.proto,也使用eventtype from b.proto。将其移至b.proto.

于 2017-07-28T20:51:45.940 回答