protoc is the Protocol Buffers compiler. It reads .proto files and generates source code in your target language, handling imports, option resolution, and plugin dispatch.
Installation
See the installation guide for platform-specific instructions. In brief:- Pre-built binary: Download
protoc-$VERSION-$PLATFORM.zipfrom the GitHub releases page. Each zip contains theprotocbinary and the standard.protofiles distributed with protobuf. - Build from source: Follow the C++ build instructions to compile protoc from source using Bazel.
- Package managers: On Windows,
vcpkg install protobufinstalls protoc alongside the runtime.
The protoc binary version and the generated code version must match for C++. See the cross-version runtime guarantee for details.
Basic usage
.proto files in a single invocation:
Output flags
Each--LANG_out flag instructs protoc to generate code for that language. Multiple output flags can be combined in a single invocation.
Passing options to an output flag
Some language generators accept options using the--LANG_out=OPT=VALUE:DST_DIR syntax:
Import paths
The--proto_path flag (short form: -I) specifies directories that protoc will search when resolving import statements. You can specify multiple import paths:
google/protobuf/timestamp.proto) are bundled with the protoc release. If you installed protoc from a zip package, add the include/ directory inside the zip to your --proto_path:
Plugin invocation
protoc has a plugin system that allows third-party code generators to be invoked as if they were built-in. Any--xxx_out flag that does not correspond to a built-in language triggers plugin lookup.
Plugin discovery
By default, protoc searches$PATH for an executable named protoc-gen-xxx, where xxx matches the flag name:
Explicit plugin path
Use--plugin to provide an explicit path instead of relying on $PATH:
Descriptor set output
protoc can serialize the parsed.proto files into a binary FileDescriptorSet without generating any language-specific code. This is useful for runtime reflection, gRPC service discovery, and tooling.
--include_imports:
All flags reference
| Flag | Description |
|---|---|
--proto_path=PATH / -I PATH | Add a directory to the import search path. Specify multiple times for multiple paths. |
--cpp_out=DST_DIR | Generate C++ header and source files. |
--java_out=DST_DIR | Generate Java source files. |
--python_out=DST_DIR | Generate Python source files. |
--csharp_out=DST_DIR | Generate C# source files. |
--ruby_out=DST_DIR | Generate Ruby source files. |
--php_out=DST_DIR | Generate PHP source files. |
--objc_out=DST_DIR | Generate Objective-C source files. |
--rust_out=DST_DIR | Generate Rust source files. |
--plugin=EXE | Specify a plugin executable, or protoc-gen-NAME=PATH to name it explicitly. |
--descriptor_set_out=FILE | Write a FileDescriptorSet (containing all input files) to the given file. |
--include_imports | Include all imported files in the --descriptor_set_out output. |
--include_source_info | Include source info (line numbers, comments) in the --descriptor_set_out output. |
--encode=MESSAGE_TYPE | Read a text-format message from stdin and write it in binary to stdout. |
--decode=MESSAGE_TYPE | Read a binary message from stdin and write it in text format to stdout. |
--version | Print the protoc version and exit. |
--help / -h | Print usage information. |