protobuf编译protoc使用说明

protobuf编译

关于protobuf的安装可以参考

将.proto文件编译为.ph.h和.pb.cc文件

编译命令:

protoc --proto_path=src --cpp_out=build/gen src/foo.proto src/bar/baz.proto

编译器会读取

src/foo.proto

src/bar/baz.proto

生成

build/gen/foo.pb.h

build/gen/foo.pb.h

build/gen/bar/baz.pb.h

build/gen/bar/baz.pb.h

其中

build/gen/bar文件夹会自动创建

build/ build/gen不会自动创建

以上内容翻译自

执行过后会发生以下变化

The names of the output files are computed by taking the name of the .proto file and making two changes: The extension (.proto) is replaced with either .pb.h or .pb.cc for the header or implementation file, respectively. The proto path (specified with the --proto_path= or -I command-line flag) is replaced with the output path (specified with the --cpp_out= flag).

三个路径的说明

刚开始接触三个路径会比较迷惑。下面意上述例子为参考解释一下每个路径的作用。ji

–proto_path=src 或-I 指定了项目路径,其实他主要是为了和后面的.proto文件路径结合,proto文件会把src去掉,并把剩下的路径/bar/baz.proto 生成相应的头文件和源文件并放到–cpp_out=build/gen路径下。

经验分享 程序员 微信小程序 职场和发展