protoc工具的使用

protoc是Protocol Buffers编译器,用于生成不同语言的代码。本文介绍了protoc的参数用法,如指定搜索目录 proto_path、显示版本信息、编码解码消息、生成C++、Java和Python代码等。同时,给出了Windows和Linux下的使用示例,强调了--proto_path参数的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下了protoc但是不知道参数怎么用。现在把各个选项翻译下:

 -IPATH, --proto_path=PATH: 

Specify the directory in which to search forimports.  May be specified multiple times;directories will be searched in order.  If notgiven, the current working directory is used

指定输入的搜索目录。可以指定多次,为了将搜索目录。如果如果不存在就使用当前的工作目录。

 --version

 Show version info and exit.

显示版本信息并退出。

 -h, --help

Show this text and exit.

显示帮助并退出。

 --encode=MESSAGE_TYPE

Read a text-format message of the given type from standard input and write it in binary to standard output.  The message type must be defined in PROTO_FILES or their imports.

        从标准输入中读取一个给定类型的文本格式,并把它写在二进制输出到标准输出。消息类型必须定义在PROTO_FILES或他们的入口。

 --decode_raw

Read an arbitrary protocol message from standard input and write the raw tag/value pairs in text format to standard output.  No PROTO_FILES should be given when using this flag.

消息从标准输入读取任意协议文本格式输出到标准输出和写入原始标记/值对。使用此标志时,应给予无PROTO_FILES。

 -oFILE,

--descriptor_set_out=FILE

Writes a FileDescriptorSet (a protocol buffer, defined in descriptor.proto) containing all of the input files to FILE.

写入FileDescriptorSet(协议缓冲区,定义descriptor.proto)包含了所有输入文件文件。

 --include_imports

When using --descriptor_set_out, also include all dependencies of the input files in the set, so that the set is self-contained.

当使用 - descriptor_set_out,还包括所有的输入文件集的依赖关系,使设定自足。

--include_source_info

When using --descriptor_set_out, do not strip SourceCodeInfo from the FileDescriptorProto. This results in vastly larger descriptors that include information about the original location of each decl in the source file as well as surrounding comments.

当使用 - descriptor_set_out,不从FileDescriptorProto剥离SourceCodeInfo。这样的结果DECL每个源文件中的原始位置以及周围的注释信息的描述符包括大得多。

 --error_format=FORMAT

Set the format in which to print errors.FORMAT may be 'gcc' (the default) or 'msvs'(Microsoft Visual Studio format). 

设置格式打印错误的格式.格式可能是“gcc”(默认)或者"msvs“(微软Visual Studio的格式)。

--plugin=EXECUTABLE

Specifies a plugin executable to use.Normally, protoc searches the PATH for plugins, but you may specify additionalexecutables not in the path using this flag.Additionally, EXECUTABLE may be of the formNAME=PATH, in which case the given plugin name is mapped to the given executable even if the executable's own name differs.

指定使用一个可执行插件。通常情况下,protoc 为插件搜索PATH路径,但你可以使用这个便令指定额外的可执行文件的路径。此外,可执行文件从PATH搜索,在这种情况下,给定的插件name是映射到甚至写明可执行自己的名字不同的可执行文件。

--cpp_out=OUT_DIR

 Generate C++ header and source.

生成c++头文件和源文件

 --java_out=OUT_DIR 

Generate Java source file.

生成java头文件和源文件


 --python_out=OUT_DIR

        Generate Python source file.

生成python源文件。

一个实例:

C:\Users\Gis_Kook>protoc --proto_path=C:\\Users\\Gis_Kook\\Desktop\\shot --cpp_out=F:\\OpenSource\\protobuf\\test C:\\Users\\Gis_Kook\\Desktop\\shot\\test.proto

比较变态的一点是必须制定--proto_path 而且其值必须在PATH环境变量里!

linux: protoc --cpp_out=./  *.proto

在 linux上依赖-lprotobuf



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值