C++ 如何使用 MinGW 在 Windows 中构建 Google 的 protobuf?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9243816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:31:44  来源:igfitidea点击:

How to build Google's protobuf in Windows using MinGW?

c++mingwprotocol-buffers

提问by Alex

I'm using Codeblocks as my IDE with MingGW. I'm trying to use google protocol buffers, but I'm having trouble building the protobuf.

我在 MingGW 中使用 Codeblocks 作为我的 IDE。我正在尝试使用 google 协议缓冲区,但是我在构建 protobuf 时遇到了问题。

The readme file for protobuf says:

protobuf 的自述文件说:

If you are using Cygwin or MinGW, follow the Unix installation instructions, above.

如果您使用的是 Cygwin 或 MinGW,请按照上面的 Unix 安装说明进行操作。

The Unix instructions says:

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

要构建和安装 C++ 协议缓冲区运行时和协议缓冲区编译器 (protoc),请执行以下操作:$ ./configure $ make $ make check $ make install

I don't know how to perform these in Windows because "configure" is a Unix script and I don't know how to execute it, or the rest of the commands.

我不知道如何在 Windows 中执行这些,因为“配置”是一个 Unix 脚本,我不知道如何执行它,或者其他命令。

Can someone explain in more detail how I can build protobuf using MinGW on Windows?

有人可以更详细地解释我如何在 Windows 上使用 MinGW 构建 protobuf 吗?

回答by Peter Remmers

Here's what worked for me:

以下是对我有用的内容:

  1. You need to install MSYS with mingw. This is a minimal unix-like shell environment that lets you configure/make most unix packages. Read the mingw docs on how to install that (either with mingw-get or the GUI installer).

  2. Once you have installed MSYS, you should have a shortcut in your start menu, named "MinGW Shell". That opens a console with a bash.

  3. Extract the source tarball to your MSYS home directory. I have mingw installed in "D:\prog", so the directory was "D:\prog\MinGW\msys\1.0\home\<username>". You can tell your MSYS username from the shell prompt. When done, you should have a directory "D:\prog\MinGW\msys\1.0\home\<username>\protobuf-2.4.1".

  4. At the shell prompt, change to the protobuf directory:

    cd protobuf-2.4.1

  5. Run the configure script (note the backquotes):

    ./configure --prefix=`cd /mingw; pwd -W`

    The --prefixparamater makes sure protobuf is installed in the mingw directory tree instead of the MSYS directories, so you can build outside the MSYS shell (e.g. with CodeBlocks...)

  6. Run make:

    make

  7. Install:

    make install

  8. That's it. You should now be able to compile your project with protobuf.
    You should be able to:

    • call protocfrom your project/makefiles
    • #include <google/protobuf/message.h>etc.
    • link with -lprotobufor -lprotobuf-lite
  1. 您需要使用 mingw 安装 MSYS。这是一个最小的类 Unix 外壳环境,可让您配置/制作大多数 Unix 包。阅读有关如何安装它的 mingw 文档(使用 mingw-get 或 GUI 安装程序)。

  2. 安装 MSYS 后,您的开始菜单中应该有一个快捷方式,名为“MinGW Shell”。这将打开一个带有 bash 的控制台。

  3. 将源 tarball 解压缩到您的 MSYS 主目录。我在“D:\prog”中安装了mingw,所以目录是“D:\prog\MinGW\msys\1.0\home\<username>”。您可以从 shell 提示中告诉您的 MSYS 用户名。完成后,您应该有一个目录“D:\prog\MinGW\msys\1.0\home\<username>\protobuf-2.4.1”。

  4. 在 shell 提示符下,切换到 protobuf 目录:

    cd protobuf-2.4.1

  5. 运行配置脚本(注意反引号):

    ./configure --prefix=`cd /mingw; pwd -W`

    --prefixparamater确保protobuf的安装在MinGW的目录树,而不是MSYS目录,这样你就可以建立MSYS壳外(例如用代码块...)

  6. 运行制作:

    make

  7. 安装:

    make install

  8. 就是这样。您现在应该能够使用 protobuf 编译您的项目。
    你应该能够:

    • protoc从您的项目/makefile调用
    • #include <google/protobuf/message.h>等等。
    • -lprotobuf或链接-lprotobuf-lite

HTH
Peter

HTH
彼得

Edit: Bringing this a bit more up to date. I tried setting up a new PC with current versions of MinGW and protobuf 2.5.0, and these are the problems I had:

编辑:使这个更新一点。我尝试使用当前版本的 MinGW 和 protobuf 2.5.0 设置一台新 PC,这些是我遇到的问题:

  1. There is no "MinGW Shell" shortcut in the start menu.
    For some reason current MinGW installations fail to install that.
    But there is a msys.batin <Mingw home>\msys\1.0which brings up a console with a bash. Create a shortcut to that batch file somewhere.

  2. gcc does not work from the MSYS shell.
    I had to run a post-installation batch file manually and answer the questions there. This sets up fstab entries that mount the mingw directories in the MSYS environment.
    You need to run <Mingw home>\msys\1.0\postinstall\pi.bat

  3. My Avira antivirus interfered with the protobuf compilation.
    It complained about the generated protoc.exe being a "TR/Crypt.XPACK.Gen" trojan and blocked acces to that file, resulting in a corrupted build.
    I got error messages saying something like protoc:./.libs/lt-protoc.c:233: FATAL: couldn't find protoc.when trying to start protoc.
    I had to disable the Avira realtime scanner and make clean && make && make installagain

  1. 开始菜单中没有“MinGW Shell”快捷方式。
    由于某种原因,当前的 MinGW 安装无法安装。
    但是有一个msys.batin <Mingw home>\msys\1.0which 会调出一个带有 bash 的控制台。在某处创建该批处理文件的快捷方式。

  2. gcc 在 MSYS shell 中不起作用。
    我必须手动运行安装后批处理文件并在那里回答问题。这将设置在 MSYS 环境中挂载 mingw 目录的 fstab 条目。
    你需要跑<Mingw home>\msys\1.0\postinstall\pi.bat

  3. 我的 Avira 防病毒软件干扰了 protobuf 编译。
    它抱怨生成的 protoc.exe 是“TR/Crypt.XPACK.Gen”木马并阻止访问该文件,导致构建损坏。
    protoc:./.libs/lt-protoc.c:233: FATAL: couldn't find protoc.在尝试启动 protoc 时收到错误消息。
    我不得不禁用Avira实时扫描仪和make clean && make && make install

Edit #2:

编辑#2:

This post has aged quite a bit, and mingw does not equal mingw anymore. In this day and age, I would rather recommend MSYS2 which comes with a port of ArchLinux's pacman package manager, a recent, better-working (c++11 std::thread support!) mingw fork for both 32 and 64 bit, and a protobuf package that you just need to install and be good.

这篇文章已经过时了,mingw 不再等于 mingw。在这个时代,我更愿意推荐 MSYS2,它带有 ArchLinux 的 pacman 包管理器的一个端口,一个最近的,更好的工作(c++11 std::thread 支持!)mingw fork,适用于 32 位和 64 位,以及一个 protobuf 包,你只需要安装它就行了。

Go hereto download!

去这里下载!

Hope this helps!
Peter

希望这可以帮助!
彼得

回答by FourtyTwo

In my case Peter's answer did not work completely, I used the latest MinGW 4.8.1 + the MSys distribution (selected both MSys packages in mingw-get).

在我的情况下,Peter 的回答并没有完全奏效,我使用了最新的 MinGW 4.8.1 + MSys 发行版(在 中选择了两个 MSys 包mingw-get)。

My problem was that the prefix didn't really work, I could only find the files in C:\MinGW\msys\1.0\local. However, after copying the bin / include / libs folders to c:\mingw,the installation worked for me, too.

我的问题是前缀并没有真正起作用,我只能在C:\MinGW\msys\1.0\local. 但是,将 bin / include / libs 文件夹复制到c:\mingw,安装后也对我有用。

回答by Jan Rosendahl

I had the same problem and i solved it by building protocol buffers using boost build. That worked fine, I can provide a jamfile for protocol buffers.

我遇到了同样的问题,我通过使用 boost build 构建协议缓冲区来解决它。效果很好,我可以为协议缓冲区提供一个 jamfile。

What I still have problems with though is to extend boost build so it generates cpp source files from proto files, but that is another story.

我仍然遇到的问题是扩展 boost build 以便它从 proto 文件生成 cpp 源文件,但这是另一回事。