gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:38:54 28 4
gpt4 key购买 nike

我有两个 .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.原型(prototype)
b.proto:文件递归导入自身:b.proto -> a.proto -> b.proto

如何实现?

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

最佳答案

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

一个.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 是有意义的。

关于protocol-buffers - 如何包含具有相互依赖性的 .proto 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45378889/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com