Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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):

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?

like image 528
bighead Avatar asked Mar 24 '11 13:03

bighead


People also ask

How do I import Protos?

To import another .proto 's definitions, you add an import statement to the top of your file: import "myproject/other_protos.proto"; By default, you can use definitions only from directly imported .proto files. However, sometimes you may need to move a .proto file to a new location.

What is a .proto file?

A . proto file is similar to a JSON file in that it represents structured data, but you can run a compiler on the . proto file to generate code that can read and write the data in the programming language of your choice. For more information about protocol buffers, see Protocol Buffer Developer Guide on Google's site.

What is the difference between proto2 and proto3?

proto3 is the current version of the language. This is the most commonly used version of the language. We encourage new code to use proto3. proto2 is an older version of the language.

How do I set default value in protobuf?

For bool s, the default value is false. For numeric types, the default value is zero. For enums , the default value is the first value listed in the enum's type definition. This means care must be taken when adding a value to the beginning of an enum value list.


1 Answers

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";

like image 112
Bruce Martin Avatar answered Oct 26 '22 04:10

Bruce Martin