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?
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.
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.
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.
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.
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).
If the imported file (language.proto) contains package types;
specify --proto_path=Parent directory and change the import to
import "types/language.proto";
If the imported file has no package
specify --proto_path=Parent directory/types and change the import to
import "language.proto";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With