To generate the Java, Kotlin, Python, C++, Go, Ruby, Objective-C, or C# code you need to work with the message types defined in a .proto
file, you need to run the protocol buffer compiler protoc
on the .proto
. If you haven't installed the compiler, download the package and follow the instructions in the README. For Go, you also need to install a special code generator plugin for the compiler: you can find this and installation instructions in the golang/protobuf repository on GitHub.
为了生成Java、Kotlin、Python、C++、Go、Ruby、Objective-C或 C#代码,你需要使用 .proto 文件中定义的消息类型,使用protocol buffer 编译器protoc
编译.proto
文件。如果你没有安装编译器 ,下载包然后按照README中的指令操作。对于Go,你还需要为编译器安装特殊的代码生成器插件:你可以在golang/protobufGitHub仓库中找到该插件和安装说明。
The Protocol Compiler is invoked as follows:
Protocol 编译器调用如下:
protoc --proto_path=IMPORT_PATH --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR --go_out=DST_DIR --ruby_out=DST_DIR --objc_out=DST_DIR --csharp_out=DST_DIR path/to/file.proto
IMPORT_PATH
specifies a directory in which to look for.proto
files when resolvingimport
directives. If omitted, the current directory is used. Multiple import directories can be specified by passing the--proto_path
option multiple times; they will be searched in order.-I=_IMPORT_PATH_
can be used as a short form of--proto_path
.
IMPORT_PATH
指定在解析import
指令时查找.proto
文件的目录。如果省略,则使用当前目录。可通过多次传入--proto_path
选项来指定多个导入目录;它们将按序查找。-I=_IMPORT_PATH_
可以用作--proto_path
的简写形式。-
You can provide one or more output directives:
你可以提供一个或多个输入指令:-
--cpp_out
generates C++ code inDST_DIR
. See the C++ generated code reference for more.
--cpp_out
在DST_DIR
目录下生成C++代码。查看更多信息请参阅C++ 生成代码参考 -
--java_out
generates Java code inDST_DIR
. See the Java generated code reference for more. -
--kotlin_out
generates additional Kotlin code inDST_DIR
. See the Kotlin generated code reference for more. -
--python_out
generates Python code inDST_DIR
. See the Python generated code reference for more. -
--go_out
generates Go code inDST_DIR
. See the Go generated code reference for more. -
--ruby_out
generates Ruby code inDST_DIR
. See the Ruby generated code reference for more. -
--objc_out
generates Objective-C code inDST_DIR
. See the Objective-C generated code reference for more. -
--csharp_out
generates C# code inDST_DIR
. See the C# generated code reference for more. -
--php_out
generates PHP code inDST_DIR
. See the PHP generated code reference for more.
As an extra convenience, if the
DST_DIR
ends in.zip
or.jar
, the compiler will write the output to a single ZIP-format archive file with the given name..jar
outputs will also be given a manifest file as required by the Java JAR specification. Note that if the output archive already exists, it will be overwritten; the compiler is not smart enough to add files to an existing archive.
作为额外的便利,如果DST_DIR
以.zip
或.jar
结尾,编译器会将输出写入具有给定名称的单个ZIP格式存档文件中。按照Java JAR规范的要求.jar
输出也会提供一个manifest 文件。注意如果输出存档已存在,它会被覆盖;编译器不够智能,无法添加文件到现有归档中。 -
You must provide one or more
.proto
files as input. Multiple.proto
files can be specified at once. Although the files are named relative to the current directory, each file must reside in one of theIMPORT_PATH
s so that the compiler can determine its canonical name.
你必须提过一个或更多.proto
文件作为输入。一次可以指定多个.proto
文件。尽管文件命名是相对于当前目录的,但每个文件也必须位于IMPORT_PATH
之一中,以便编译器决定其规范名称。