← 返回命令列表

Linux command

protoc 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Generate Python code

protoc --python_out=[path/to/output_directory] [input_file.proto]

Generate Java code

protoc --java_out=[path/to/output_directory] --proto_path=[path/to/import_search_path] [input_file.proto]

Generate Go code

protoc --go_out=[path/to/output_directory] --go_opt=paths=source_relative [input_file.proto]

Encode

protoc < [message.txt] --encode=[TypeName] [input_file.proto]

Decode a protocol message

protoc < [message.bin] --decode=[TypeName] [input_file.proto]

Decode a protocol message

protoc < [message.bin] --decode_raw

说明

protoc is the official compiler for Google's Protocol Buffers, a language-neutral serialization framework. It reads .proto definition files that describe structured data formats and generates language-specific source code for serializing and deserializing that data efficiently in C++, Java, Python, Go, and many other languages through plugins. The compiler resolves imports between .proto files using search paths specified with -I (or --proto_path), and can generate code for multiple target languages in a single invocation. It also supports encoding and decoding protocol buffer messages on the command line for debugging and testing purposes. protoc is a core component of the gRPC ecosystem, where .proto files define both data structures and service interfaces for remote procedure calls.

FAQ

What is the protoc command used for?

protoc is the official compiler for Google's Protocol Buffers, a language-neutral serialization framework. It reads .proto definition files that describe structured data formats and generates language-specific source code for serializing and deserializing that data efficiently in C++, Java, Python, Go, and many other languages through plugins. The compiler resolves imports between .proto files using search paths specified with -I (or --proto_path), and can generate code for multiple target languages in a single invocation. It also supports encoding and decoding protocol buffer messages on the command line for debugging and testing purposes. protoc is a core component of the gRPC ecosystem, where .proto files define both data structures and service interfaces for remote procedure calls.

How do I run a basic protoc example?

Run `protoc --python_out=[path/to/output_directory] [input_file.proto]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

Where can I find more protoc examples?

This page includes 6 examples for protoc, plus related commands for nearby Linux tasks.