从其他文件导入消息时 Protobuf 编译错误
Protobuf Compilation error while importing messages from other files
我有 2 个 protobuf 文件文件 -
file1.proto
**package abcd.xyz**
message Msg1{
uint32 id =1;
string name = 2;
}
file2.proto
**package com.abcd.xyz**
import "abcd/xyz/Msg1.proto";
message Msg2{
uint32 id =1;
string name = 2;
Msg1 msg = 3;
}
当我编译这个时,出现以下错误 -
[exec] com/abcd/xyz/Msg2.proto:5: "abcd.xyz.Msg1" is resolved to "com.abcd.xyz.Msg1", which is not defined. The innermost scope is searched first in name resolution. Consider using a leading '.'(i.e., ".abcd.xyz.Msg1") to start from the outermost scope.
Msg1.proto 文件在多个地方使用,我无法更改它的包。
知道在不更改包名称的情况下编译 Msg2.proto 文件需要做哪些更改吗?
这是我能够 运行 的示例,希望能帮助您解决问题。当我查看您的代码时,我注意到您导入了包,但没有在消息 Msg2 的定义中指定 Msg1 的包位置。为确保您理解我告诉您的内容,让我向您展示一个示例,您的代码在执行此操作后将如何显示:
现在我有当前的项目目录:
testprotoc
- test
- file1.proto
- xyz
- file2.proto
其中test和xyz是testprotoc主包的内部包。
file1.proto 如下所示:
syntax = "proto3";
package testprotoc.test;
message Msg1 {
uint32 id =1;
string name = 2;
}
因此,您指定了协议缓冲区包和 protobuf 将要使用的语法。然后我们有 file2.proto:
syntax = "proto3";
package testprotoc.xyz;
import "test/file1.proto";
message Msg2 {
uint32 id =1;
string name = 2;
test.Msg1 msg = 3;
}
我们有相同的东西、语法、包和导入。 内部包的相对导入,不包括主要包。
注意我在使用的时候指定了消息定义的包。这就是您遗漏的要点,这就是解决它的方法!
我有 2 个 protobuf 文件文件 -
file1.proto
**package abcd.xyz**
message Msg1{
uint32 id =1;
string name = 2;
}
file2.proto
**package com.abcd.xyz**
import "abcd/xyz/Msg1.proto";
message Msg2{
uint32 id =1;
string name = 2;
Msg1 msg = 3;
}
当我编译这个时,出现以下错误 -
[exec] com/abcd/xyz/Msg2.proto:5: "abcd.xyz.Msg1" is resolved to "com.abcd.xyz.Msg1", which is not defined. The innermost scope is searched first in name resolution. Consider using a leading '.'(i.e., ".abcd.xyz.Msg1") to start from the outermost scope.
Msg1.proto 文件在多个地方使用,我无法更改它的包。
知道在不更改包名称的情况下编译 Msg2.proto 文件需要做哪些更改吗?
这是我能够 运行 的示例,希望能帮助您解决问题。当我查看您的代码时,我注意到您导入了包,但没有在消息 Msg2 的定义中指定 Msg1 的包位置。为确保您理解我告诉您的内容,让我向您展示一个示例,您的代码在执行此操作后将如何显示:
现在我有当前的项目目录:
testprotoc
- test
- file1.proto
- xyz
- file2.proto
其中test和xyz是testprotoc主包的内部包。
file1.proto 如下所示:
syntax = "proto3";
package testprotoc.test;
message Msg1 {
uint32 id =1;
string name = 2;
}
因此,您指定了协议缓冲区包和 protobuf 将要使用的语法。然后我们有 file2.proto:
syntax = "proto3";
package testprotoc.xyz;
import "test/file1.proto";
message Msg2 {
uint32 id =1;
string name = 2;
test.Msg1 msg = 3;
}
我们有相同的东西、语法、包和导入。 内部包的相对导入,不包括主要包。
注意我在使用的时候指定了消息定义的包。这就是您遗漏的要点,这就是解决它的方法!