如何 运行 Windows 上的 protobuf python 插件

How to run a protobuf python plugin on Windows

我目前正在使用 Protobuf 插件生成一些给定一组 Protobuf 文件的自定义 C# 代码。它在 Linux 上 运行 很好,我也想在 Windows 上 运行 它,以便直接从我的 Visual Studio 项目生成此代码。

这是我目前(未成功)使用的命令行:

path\to\protoc.exe --plugin=protoc-gen-my-plugin=path\to\my-plugin.py --my-plugin_out=output\path\gen my_proto.proto

这是我遇到的错误:

--my-plugin_out: protoc-gen-my-plugin: %1 n'est pas une application Win32 valide.

我已经安装了 Python 2.7.11,它在我的路径中。我还尝试 运行 protoc.exe 同时使用 x86 和 x64 可执行文件。

是否有任何我不知道的限制或我错过了什么?

我能够通过将 python 插件封装在 .bat 文件中来解决我的问题。这是我使用的代码:

@echo off
chdir path\to\my-plugin
python -u my-plugin.py

运行 python 脚本时的“-u”选项非常重要,否则标准输入将被缓冲。由于 protoc 通过 stdin 将所有输入传递给插件(要解析的 .proto 文件),这非常重要。输出由插件写入标准输出,所以没有问题。

这是使用我的插件执行 protoc 的最终命令:

path\to\protoc.exe --plugin=protoc-gen-my-plugin=redirect.bat --my-plugin_out=output\path\gen my_proto.proto