简体   繁体   English

在Protocol Buffers中,如何从上层目录导入文件?

[英]In Protocol Buffers, how to import a file from the upper level directory?

I have the following code in a protocol buffer file(pcfg_lm.proto): 我在协议缓冲区文件(pcfg_lm.proto)中有以下代码:

import "../types/language.proto";

package nlp;

message PCFGProto {
  required Language lang = 1;
}

And of course there is a proto file exists at ../types/language.proto. 当然,在../types/language.proto中存在一个proto文件。 However, when I issue the command: 但是,当我发出命令时:

protoc pcfg_lm.proto --cpp_out=/tmp

Here is the error message: 这是错误消息:

../types/language.proto: File not found.
pcfg_lm.proto: Import "../types/language.proto" was not found or had errors.
pcfg_lm.proto:6:12: "Language" is not defined.

I think there must be some way to specify the file names in the upper level directories, without using the -I flag. 我认为必须有一些方法来指定上层目录中的文件名,而不使用-I标志。 But how do I do that? 但是我该怎么做?

You can use the --proto_path= directive to specify which directories to search for imports. 您可以使用--proto_path =指令指定要搜索导入的目录。 It can be used multiple times if needed. 如果需要,可以多次使用。

The correct --proto_path will depend on how the package is defined in the imported file (language.proto). 正确的--proto_path将取决于如何在导入的文件(language.proto)中定义包。

  1. If the imported file (language.proto) contains package types; 如果导入的文件(language.proto)包含包类型;

    specify --proto_path= Parent directory and change the import to 指定--proto_path = 父目录并将导入更改为

    import "types/language.proto"; import“types / language.proto”;

  2. If the imported file has no package 如果导入的文件没有包

    specify --proto_path= Parent directory/types and change the import to 指定--proto_path = 父目录/类型并将导入更改为

    import "language.proto"; import“language.proto”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM