在Windows上安装Protocol Buffers

50

我无法找到关于在Windows x64平台上安装Google Protocol Buffers(包括编译器)的清晰指示。

我已经阅读了编译器和源代码的README文件:

对于编译器:只需将此二进制文件放置在PATH中的某个位置即可安装

我已将系统变量添加到Path中:

PROTOC 'C:\dev_tools\protoc-2.4.1-win32'

我卡在使用Cygwin安装Protocol Buffers源代码上。我尝试按照自述文件中提供的Unix说明进行操作:

To build and install the C++ Protocol Buffer runtime and the Protocol Buffer compiler (protoc) execute the following:

$ ./configure
$ make
$ make check
$ make install

If make check fails, you can still install, but it is likely that some features of this library will not work correctly on your system. Proceed at your own risk.

make install may require superuser privileges.

For advanced usage information on configure and make, see INSTALL.txt.

** Hint on install location **

By default, the package will be installed to /usr/local. However, on many platforms, /usr/local/lib is not part of LD_LIBRARY_PATH. You can add it, but it may be easier to just install to /usr> instead. To do this, invoke configure as follows:

./configure --prefix=/usr
我得到
-bash: ./configure: No such file or directory'

请问有没有人能够提供清晰详细的步骤来使这个工作起来?

更新

我改用MSYS/MINGW32,并按照此link中给出的说明进行操作。现在我卡在了以下问题上:

当我运行'./configure'命令时,我收到以下错误:
checking how to run the C++ preprocessor... /lib/cpp
configure: error: C++ preprocessor "/lib/cpp" fails sanity check'

因此,所有的make、make install命令都无法工作。例如:

make: No targets specified and no makefile found. Stop.

23
安装这个东西的文档写得如此之差,令人惊讶。真是可悲。 - basickarl
10
2019年以来,谷歌关于在Windows上设置协议缓冲区的文档仍然很糟糕。 - LuckyLuc
8
2020年过去了,情况没怎么变。 - Andreas
1
明天就要到2022年了,但情况并没有好转多少。 - mikro
11个回答

26

如果您只想编译ProtoBuf定义文件,您可以在ProtoBuf GitHub发布页面上直接下载适用于所有平台的protoc预编译二进制文件

自2015年以来,它们就已经有预编译二进制文件了,但在众多下载中很容易被忽略。


2
但是在众多下载中很容易忽略它们。是的! - root-11
非常感谢。好的!那真的是正确的!它们在最后面! - Hossein
1
预编译二进制文件不适用于C++。 - MorioBoncz
@MorioBoncz 它不在 protobuf-cpp-3.13.0.tar.gz 中吗?我没有检查,只是根据名称去的。 - Florian Sesser
请注意,我的回答是关于protoc,即protobuf编译器的,因为这是我一个团队成员机器上缺少的二进制文件,而且OP似乎也缺少了这个。其余部分,请将其包含在您的正常构建过程中,因为您必须链接到库。 - Florian Sesser
显示剩余2条评论

24

这个在Windows上对我很有帮助,谢谢!顺便说一下,你可以直接从目标位置运行./protoc作为二进制文件,而不是使用路径变量。 - Guy_g23
这个在Windows上帮了我,谢谢!顺便说一下,你可以直接从目标位置运行./protoc作为一个二进制文件,而不是使用路径变量。 - Guy_g23
当您在下载页面上找不到protoc时,只需在Assets下方点击“显示所有28个资产”。您只能在顶部看到protobuf下载。 - Balu

18

在Windows上编译protobuf有完整的文档:

您需要7-zip、Cmake和Visual Studio。

无论如何,使用持续集成工具(如Travis或Appveyor)的一个意外副作用是,总是有最新的和可用的构建脚本。我通常查看appveyor.ymltravis_config.yml文件(如果存在)。

>>> git clone -b v3.1.0 https://github.com/google/protobuf.git 
>>> cd protobuf
>>> curl -L -o release-1.7.0.zip https://github.com/google/googlemock/archive/release-1.7.0.zip
>>> 7z x release-1.7.0.zip
>>> del /Q release-1.7.0.zip
>>> rename googlemock-release-1.7.0 gmock
>>> curl -L -o release-1.7.0.zip "https://github.com/google/googletest/archive/release-1.7.0.zip"
>>> 7z x release-1.7.0.zip
>>> del /Q release-1.7.0.zip
>>> rename googletest-release-1.7.0 gtest
>>> move gtest gmock
>>> set generator=Visual Studio 12 Win64
>>> set vcplatform=x64
>>> mkdir build_msvc
>>> cd build_msvc
>>> cmake -G "%generator%" -Dprotobuf_BUILD_SHARED_LIBS=%BUILD_DLL% -Dprotobuf_UNICODE=%UNICODE% ../cmake
>>> msbuild protobuf.sln /p:Platform=%vcplatform% || goto error

为了解决7z.exeMsbuild.exe可执行文件的路径问题,您需要使用curlGit Bash中已经包含了它)。

我成功地在Windows 10 x64机器上使用Visual Studio 2015构建了protobuf编译器。


2
它是否适用于Java的protobuf编译器安装?看起来你正在提供安装C++编译器的解决方案。 - Rajeev Kumar
1
没有Java的protobuf编译器:编译器是用C++编写的,有一个Java绑定嵌入了结果运行时。我没有测试过Java绑定,因为我不熟悉Java和Maven。 - lucasg

14

6

请注意,二进制文件和其他文件都在“资产”部分下,该部分默认情况下未展开。因此,请单击“资产”旁边的小向下箭头。 - Richard Keene

4

以下是我在Windows 10上构建protobuf v2.4.1的步骤:

  • 克隆代码库:https://github.com/protocolbuffers/protobuf.git
  • 进入protobuf目录;
  • 检出v2.4.1分支;
  • 进入vsprojects目录;
  • 用Visual Studio 2019打开protobuf.sln文件;
  • 执行“生成解决方案”,会报错:未定义min和未定义max。在protobuf/stubs/common.h文件中添加以下代码:
#if defined(_WIN32) && !defined(min)
#define min(a,b) __min(a,b)
#define max(a,b) __max(a,b)
#endif
  • 按下“build solution”按钮,出现错误:fatal error C1189:“#error:hash_map已被弃用并将被删除……
  • 添加编译定义-D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS解决问题
  • 接下来出现错误“error C2664:“std::pair std::make_pair(_Ty1 &&,_Ty2 &&) noexcept”……
  • 在文件src\google\protobuf\compiler\command_line_interface.cc中,我修改了字符串“proto_path_.push_back(make_pair <string, string>(virtual_path, disk_path));”为“proto_path_.push_back(make_pair(virtual_path, disk_path));
  • 按下“build solution”按钮。所有构建都成功。(没有测试框架的测试项目不会构建)

    安装:

  • 运行extract_includes.bat将所有公共头文件复制到单独的“include”目录中(在顶层软件包目录下)。
  • 将include目录的内容复制到您想要放置头文件的位置
  • 将protoc.exe复制到您放置构建工具的位置
  • 将libprotobuf.lib、libprotobuf-lite.lib和libprotoc.lib复制到您放置库的位置。

3

我使用Chocolatey安装它,它完美运行。

choco --install -y protoc

1
我建议在Windows上使用vcpkg工具。这里是逐步手册。关于protobuf,首先检查您有哪些选项(在cmd中):vcpkg search protobuf

enter image description here

接下来安装所需的包: vcpkg install protobuf:x64-windows-static

注意冒号后面的 x64-windows-static - 这是 三元组。查看 vcpkg help triplet 获取所有三元组信息。

然后进入 your_path\vcpkg-master\packages\protobuf_x64-windows-static\ 目录。

enter image description here

现在您可以设置环境变量。

您可以通过省略“-static”部分来安装库的静态和动态版本。 - nass

0

choco install protoc --pre 这个命令可行,确保你处于提升状态。

这是我在 gitbash 上运行 protoc 时的输出:

$ protoc Usage: C:\ProgramData\chocolatey\lib\protoc\tools\bin\protoc.exe [OPTION] PROTO_FILES 根据给定的选项解析 PROTO_FILES 并生成输出: -IPATH, --proto_path=PATH 指定搜索目录

将它添加到我的 $path 环境变量没有按照我预期的那样起作用。


0

现在protobuf是Visual Studio中的一个NuGet包。只需获取它即可。


1
使用vcpk安装时出现以下错误 `调用堆栈(最近的调用在最前面): scripts/cmake/vcpkg_configure_cmake.cmake:273 (vcpkg_execute_required_process) ports/protobuf/portfile.cmake:44 (vcpkg_configure_cmake) scripts/ports.cmake:71 (include)错误:构建包protobuf:x86-windows失败:BUILD_FAILED` - abdou_dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接