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

删除回忆录丶 提交于 2019-12-18 11:53:49

问题


I have the following code in a protocol buffer file(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. 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. But how do I do that?


回答1:


You can use the --proto_path= directive to specify which directories to search for imports. 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).

  1. If the imported file (language.proto) contains package types;

    specify --proto_path=Parent directory and change the import to

    import "types/language.proto";

  2. If the imported file has no package

    specify --proto_path=Parent directory/types and change the import to

    import "language.proto";



来源:https://stackoverflow.com/questions/5420169/in-protocol-buffers-how-to-import-a-file-from-the-upper-level-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!