protobuf version: 3.2
System: Ubuntu 16.04
When --proto-path is specified twice with certain setup, the --cpp_out will not be the direct output directory.
Set up
~/project/proto/main.proto~/project/proto/shared.protoProcess
~/project/build//usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/ -I ~/project/proto/ ~/project/proto/hello.protoResult
Output files are under ~/project/build/proto/ but not ~/project/build/, which is unexpected.
/usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/proto/ ~/project/proto/hello.protoResult
Output files are under ~/project/build/, which is expected.
The correct command line parameter for --cpp_out is: protoc --cpp_out=~/project/build/
@adhesiveNOR both forms are valid.
Part of the mechanism is explained in :
https://github.com/google/protobuf/issues/3044
Seems like searching in ipath is recursive. It goes deep down to the every leaf of directory tree. Multiple ipath are assigned priorities.
The directory layout of generated files is determined by their import name (the name you use in the 'import "path/to/other.proto";' statement to import that .proto file) and the import name as I explained in https://github.com/google/protobuf/issues/3044 is determined by matching the .proto file path against the --proto_path flags in order.
For
/usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/ -I ~/project/proto/ ~/project/proto/hello.proto
The import name is "proto/hello.proto" because ~/project/proto/hello.proto matches the first "-I ~/project", and as such protoc will generate ~/project/build/proto/hellp.pb.h|cc.
If you instead do
/usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/proto -I ~/project ~/project/proto/hello.proto
The import name will be "hello.proto" because "-I ~/project/proto" will be matched first.
Usually it's better to just put all .proto files in a single directory tree and specify the -I flag only once on the command line. More complex setup is usually not necessary and is very easy to get it wrong.
Most helpful comment
The directory layout of generated files is determined by their import name (the name you use in the 'import "path/to/other.proto";' statement to import that .proto file) and the import name as I explained in https://github.com/google/protobuf/issues/3044 is determined by matching the .proto file path against the --proto_path flags in order.
For
The import name is "proto/hello.proto" because ~/project/proto/hello.proto matches the first "-I ~/project", and as such protoc will generate ~/project/build/proto/hellp.pb.h|cc.
If you instead do
The import name will be "hello.proto" because "-I ~/project/proto" will be matched first.
Usually it's better to just put all .proto files in a single directory tree and specify the -I flag only once on the command line. More complex setup is usually not necessary and is very easy to get it wrong.