简体   繁体   English

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

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

I'm using Codeblocks as my IDE with MingGW.我使用 Codeblocks 作为我的 IDE 和 MingGW。 I'm trying to use google protocol buffers, but I'm having trouble building the protobuf.我正在尝试使用 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 脚本,我不知道如何执行它,或者命令的 rest。

Can someone explain in more detail how I can build protobuf using MinGW on Windows?有人可以更详细地解释我如何在 Windows 上使用 MinGW 构建 protobuf 吗?

Here's what worked for me: 这对我有用:

  1. You need to install MSYS with mingw. 您需要使用mingw安装MSYS。 This is a minimal unix-like shell environment that lets you configure/make most unix packages. 这是一个类似于unix的最小shell环境,允许您配置/生成大多数unix包。 Read the mingw docs on how to install that (either with mingw-get or the GUI installer). 阅读有关如何安装它的mingw文档(使用mingw-get或GUI安装程序)。

  2. Once you have installed MSYS, you should have a shortcut in your start menu, named "MinGW Shell". 一旦安装了MSYS,就应该在开始菜单中有一个名为“MinGW Shell”的快捷方式。 That opens a console with a bash. 这会打开一个带有bash的控制台。

  3. Extract the source tarball to your MSYS home directory. 将源tarball解压缩到MSYS主目录。 I have mingw installed in "D:\\prog", so the directory was "D:\\prog\\MinGW\\msys\\1.0\\home\\<username>". 我在“D:\\ prog”中安装了mingw,因此目录是“D:\\ prog \\ MinGW \\ msys \\ 1.0 \\ home \\ <username>”。 You can tell your MSYS username from the shell prompt. 您可以从shell提示符中告诉您的MSYS用户名。 When done, you should have a directory "D:\\prog\\MinGW\\msys\\1.0\\home\\<username>\\protobuf-2.4.1". 完成后,您应该有一个目录“D:\\ prog \\ MinGW \\ msys \\ 1.0 \\ home \\ <用户名> \\ protobuf-2.4.1”。

  4. At the shell prompt, change to the protobuf directory: 在shell提示符下,切换到protobuf目录:

    cd protobuf-2.4.1

  5. Run the configure script (note the backquotes): 运行configure脚本(注意反引号):

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

    The --prefix paramater makes sure protobuf is installed in the mingw directory tree instead of the MSYS directories, so you can build outside the MSYS shell (eg with CodeBlocks...) --prefix参数确保protobuf安装在mingw目录树而不是MSYS目录中,因此您可以在MSYS shell外部构建(例如使用CodeBlocks ...)

  6. Run make: 运行make:

    make

  7. Install: 安装:

    make install

  8. That's it. 而已。 You should now be able to compile your project with protobuf. 您现在应该能够使用protobuf编译项目。
    You should be able to: 你应该能够:

    • call protoc from your project/makefiles 从你的项目/ makefile中调用protoc
    • #include <google/protobuf/message.h> etc. #include <google/protobuf/message.h>
    • link with -lprotobuf or -lprotobuf-lite 链接-lprotobuf-lprotobuf-lite

HTH HTH
Peter 彼得

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. 开始菜单中没有“MinGW Shell”快捷方式。
    For some reason current MinGW installations fail to install that. 由于某些原因,当前的MinGW安装无法安装。
    But there is a msys.bat in <Mingw home>\\msys\\1.0 which brings up a console with a bash. 但是在<Mingw home>\\msys\\1.0有一个msys.bat会打开一个带有bash的控制台。 Create a shortcut to that batch file somewhere. 在某处创建该批处理文件的快捷方式。

  2. gcc does not work from the MSYS shell. gcc无法从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. 这将设置在MSYS环境中挂载mingw目录的fstab条目。
    You need to run <Mingw home>\\msys\\1.0\\postinstall\\pi.bat 你需要运行<Mingw home>\\msys\\1.0\\postinstall\\pi.bat

  3. My Avira antivirus interfered with the protobuf compilation. 我的Avira防病毒软件干扰了protobuf编译。
    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. 它抱怨生成的protoc.exe是一个“TR / Crypt.XPACK.Gen”木马并阻止了对该文件的访问,导致构建损坏。
    I got error messages saying something like protoc:./.libs/lt-protoc.c:233: FATAL: couldn't find protoc. 我收到类似protoc:./.libs/lt-protoc.c:233: FATAL: couldn't find protoc.错误消息protoc:./.libs/lt-protoc.c:233: FATAL: couldn't find protoc. when trying to start protoc. 当试图启动protoc时。
    I had to disable the Avira realtime scanner and make clean && make && make install again 我不得不禁用Avira实时扫描程序,然后再次make clean && make && make install

Edit #2: 编辑#2:

This post has aged quite a bit, and mingw does not equal mingw anymore. 这个帖子已经老了很多,而且mingw不再等于mingw了。 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. 在这个时代,我宁愿推荐MSYS2,它带有一个ArchLinux的pacman包管理器的端口,一个最新的,更好用的(c ++ 11 std :: thread support!)mingw fork for 32和64 bit,and一个protobuf包,你只需要安装并保持良好状态。

Go here to download! 去这里下载!

Hope this helps! 希望这可以帮助!
Peter 彼得

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 ). 在我的情况下,彼得的答案没有完全奏效,我使用了最新的MinGW 4.8.1 + MSys发行版(在mingw-get选择了两个MSys包)。

My problem was that the prefix didn't really work, I could only find the files in C:\\MinGW\\msys\\1.0\\local . 我的问题是前缀没有真正起作用,我只能找到C:\\MinGW\\msys\\1.0\\local However, after copying the bin / include / libs folders to c:\\mingw, the installation worked for me, too. 但是,在将bin / include / libs文件夹复制到c:\\mingw,安装也适用于我。

LATEST ANSWER最新答案

Here is an answer for the present-day versions of protobuf source distribution, for using protobuf for C++ language in Windows.这是当前版本的 protobuf 源分发的答案,用于在 Windows 中将 protobuf 用于 C++ 语言。 (My protobuf version is 21.4- libprotoc 3.21.4 ) (我的 protobuf 版本是 21.4- libprotoc 3.21.4

Referring to the answer by @peter-remmers参考@peter-remmers 的答案

Step 0: Download protobuf zip file from release page .第 0 步: 从发布页面下载 protobuf zip 文件 eg "protobuf-cpp-3.21.4.zip"例如“protobuf-cpp-3.21.4.zip”

  • Extract it to a path where you want protobuf to be installed.将其解压缩到要安装 protobuf 的路径。
  • Add the "src" folder path to system's environment path variables.将“src”文件夹路径添加到系统的环境路径变量中。 eg "C:\Path-To-Protobuf\protobuf-3.21.4\src"例如“C:\Path-To-Protobuf\protobuf-3.21.4\src”

Step 1: Download & install msys2: https://www.msys2.org/第1步:下载并安装msys2: https://www.msys2.org/

Make sure to to do these:确保执行以下操作:

  • pacman -Syu
  • Run "MSYS2 MSYS" from Start menu.从开始菜单运行“MSYS2 MSYS”。 Update the rest of the base packages with pacman -Syu使用pacman -Syu更新基础包的 rest
  • pacman -S --needed base-devel mingw-w64-x86_64-toolchain

Step 2: Add the mingw bin files' path to system's environment variables.第 2 步:将 mingw bin 文件的路径添加到系统的环境变量中。

  • eg it was "C:\Path-To-Msys2\msys2\mingw64\bin" and "C:\Path-To-Msys2\msys2\usr\bin"例如,它是“C:\Path-To-Msys2\msys2\mingw64\bin”和“C:\Path-To-Msys2\msys2\usr\bin”
  • Confirm by Checking the g++ version: g++ --version in the terminal (eg Mine is 12.1.0)通过检查 g++ 版本来确认: g++ --version在终端(例如我的是 12.1.0)

Step 3: Setting Up C++ runtime with protobuf libraries:第 3 步:使用 protobuf 库设置 C++ 运行时:

  • So, back in msys2 , install protobuf's libraries: pacman -S mingw-w64-x86_64-protobuf .所以,回到msys2 ,安装 protobuf 的库: pacman -S mingw-w64-x86_64-protobufReference参考
  • Now, change the directory to path where protobuf is installed in your system: eg cd "C:\Path-To-Protobuf\protobuf-3.21.4"现在,将目录更改为系统中安装 protobuf 的路径:例如 cd "C:\Path-To-Protobuf\protobuf-3.21.4"
  • Run the configure file of protobuf: ./configure运行protobuf的配置文件: ./configure
  • Run make (In case of any error with aclocal, etc. Try running pacman -S autoconf , then try again make )运行make (如果 aclocal 等出现任何错误,请尝试运行pacman -S autoconf ,然后再试一次make
  • Run make install运行make install

Step 4: That's it.第4步:就是这样。 You should now be able to compile your project with protobuf.您现在应该能够使用 protobuf 编译您的项目。

eg To compile a.proto file using protoc to cpp code & header files:例如,使用 protoc 将 a.proto 文件编译为 cpp 代码和 header 文件:

  • Use: protoc --cpp_out=$OUTDIR example.proto使用: protoc --cpp_out=$OUTDIR example.proto
  • Two files, a pb.cc & a pb.h file will be generated.将生成两个文件,一个pb.cc和一个pb.h文件。
  • Write a cpp code to use that header file and create objects, populate data, etc.编写一个 cpp 代码以使用该 header 文件并创建对象、填充数据等。
  • Compile that cpp writer file from terminal/PowerShell like:从终端/PowerShell 编译该 cpp 编写器文件,例如:

g++ -I "C:\Path-To-Protobuf\protobuf-3.21.4\src" "Path-To-Code\writer.cpp" "Path-To-Code\example.pb.cc" -o "Path-To-Code\writer.exe" -L "C:\Path-To-Protobuf\protobuf-3.21.4\src\.libs" -lprotobuf -pthread

("-pthread" is not really important at the end I guess.) (我猜“-pthread”最后并不重要。)

NOTE (The problem I had): Order of paths of eg mingw in system's environment variables list matters very much.注意(我遇到的问题):系统环境变量列表中例如 mingw 的路径顺序非常重要。

I had the same problem and i solved it by building protocol buffers using boost build. 我遇到了同样的问题,我通过使用boost build构建协议缓冲区来解决它。 That worked fine, I can provide a jamfile for protocol buffers. 这很好,我可以为协议缓冲区提供一个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构建,以便从proto文件生成cpp源文件,但这是另一个故事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM