如何使用 MinGW 在 Windows 中构建 Google 的 protobuf?
我使用 Codeblocks 作为 MingGW 的 IDE。 我正在尝试使用谷歌协议缓冲区, 但我在构建 protobuf 时遇到了麻烦。
protobuf 的自述文件说:
如果您使用的是 Cygwin 或 MinGW, 按照上面的 Unix 安装说明进行操作。
Unix 指令说:
构建并安装 C++ Protocol Buffer 运行时和协议 缓冲区编译器(协议)执行以下内容:
$ ./配置 $ 制作 $ 进行检查 $ make install
我不知道如何在 Windows 中执行这些 因为“configure”是一个 Unix 脚本并且 我不知道如何执行它或其余命令。
有人可以更详细地解释一下如何 我可以在 Windows 上使用 MinGW 构建 protobuf 吗?
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.
The readme file for protobuf says:
If you are using Cygwin or MinGW,
follow the Unix installation instructions, above.
The Unix instructions says:
To build and install the C++ Protocol Buffer runtime and the Protocol
Buffer compiler (protoc) execute the following:
$ ./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.
Can someone explain in more detail how
I can build protobuf using MinGW on Windows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是对我有用的:
您需要使用 mingw 安装 MSYS。这是一个最小的类 UNIX shell 环境,可让您配置/制作大多数 UNIX 软件包。阅读 mingw 文档了解如何安装它(使用 mingw-get 或 GUI 安装程序)。
安装 MSYS 后,开始菜单中应该有一个名为“MinGW Shell”的快捷方式。这将使用 bash 打开控制台。
将源 tarball 解压到您的 MSYS 主目录。我将 mingw 安装在“D:\prog”中,因此目录为“D:\prog\MinGW\msys\1.0\home\<用户名>”。您可以从 shell 提示符中得知您的 MSYS 用户名。完成后,您应该有一个目录“D:\prog\MinGW\msys\1.0\home\\protobuf-2.4.1”。
在 shell 提示符下,切换到 protobuf 目录:
cd protobuf-2.4.1
运行配置脚本(注意反引号):
<代码>./configure --prefix=`cd /mingw; pwd -W`
--prefix
参数确保 protobuf 安装在 mingw 目录树中而不是 MSYS 目录中,因此您可以在 MSYS shell 之外构建(例如使用 CodeBlocks...)运行 make:
make
安装:
make install
就是这样。您现在应该能够使用 protobuf 编译您的项目。
您应该能够:
protoc
#include
等-lprotobuf
或-lprotobuf-lite
链接HTH
彼得
编辑:
让这个更新一点。我尝试使用当前版本的 MinGW 和 protobuf 2.5.0 设置一台新 PC,这些是我遇到的问题:
开始菜单中没有“MinGW Shell”快捷方式。
由于某种原因,当前的 MinGW 安装无法安装它。
但是
\msys\1.0
中有一个msys.bat
,它会打开带有 bash 的控制台。在某处创建该批处理文件的快捷方式。gcc 无法在 MSYS shell 中运行。\msys\1.0\postinstall\pi.bat
我必须手动运行安装后批处理文件并回答那里的问题。这将设置在 MSYS 环境中挂载 mingw 目录的 fstab 条目。
您需要运行
我的 Avira 防病毒软件干扰了 protobuf 编译。
它抱怨生成的 protoc.exe 是“TR/Crypt.XPACK.Gen”木马并阻止对该文件的访问,导致构建损坏。
尝试启动 protoc 时,我收到错误消息,内容类似于
protoc:./.libs/lt-protoc.c:233: FATAL: Could not find protoc.
。我必须禁用 Avira 实时扫描仪并
make clean &&制作&&再次进行安装
编辑#2:
这篇文章已经过时了很多,并且 mingw 不再等于 mingw。
在当今时代,我宁愿推荐 MSYS2,它附带了 ArchLinux 的 pacman 包管理器的端口,这是一个最近的、工作更好的(c++11 std::thread 支持!) mingw fork,适用于 32 位和 64 位,并且一个 protobuf 包,你只需要安装并使用即可。
前往此处下载!
希望这有帮助!
彼得
Here's what worked for me:
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).
Once you have installed MSYS, you should have a shortcut in your start menu, named "MinGW Shell". That opens a console with a bash.
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".
At the shell prompt, change to the protobuf directory:
cd protobuf-2.4.1
Run the configure script (note the backquotes):
./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 (e.g. with CodeBlocks...)Run make:
make
Install:
make install
That's it. You should now be able to compile your project with protobuf.
You should be able to:
protoc
from your project/makefiles#include <google/protobuf/message.h>
etc.-lprotobuf
or-lprotobuf-lite
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:
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.bat
in<Mingw home>\msys\1.0
which brings up a console with a bash. Create a shortcut to that batch file somewhere.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
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 install
againEdit #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.
Go here to download!
Hope this helps!
Peter
就我而言,Peter 的回答并没有完全起作用,我使用了最新的 MinGW 4.8.1 + MSys 发行版(在
mingw-get
中选择了两个 MSys 软件包)。我的问题是前缀并没有真正起作用,我只能在
C:\MinGW\msys\1.0\local
中找到文件。但是,将 bin / include / libs 文件夹复制到c:\mingw
后,安装也对我有用。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
).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 toc:\mingw,
the installation worked for me, too.我遇到了同样的问题,我通过使用 boost build 构建协议缓冲区解决了它。
效果很好,我可以为协议缓冲区提供一个 jamfile。
不过,我仍然遇到的问题是扩展 boost 构建,以便它从 proto 文件生成 cpp 源文件,但那是另一个故事了。
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.
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.
最新答案
这是当前版本的 protobuf 源代码分发的答案,用于在 Windows 中使用 protobuf 进行 C++ 语言。
(我的protobuf版本是21.4-libprotoc 3.21.4)
参考@peter的答案 -remmers
第 0 步: 下载 protobuf 发布页面中的 zip 文件。例如“protobuf-cpp-3.21.4.zip”
第 1 步: 下载并下载安装 msys2:https://www.msys2.org/
确保执行以下操作:
pacman -Syu
pacman -Syu
更新其余基础包pacman -S --needed base-devel mingw-w64-x86_64-toolchain
第 2 步: 将mingw bin文件的路径添加到系统的环境变量中。
g++ --version
在终端中(例如我的是 12.1.0)第 3 步:使用 protobuf 库设置 C++ 运行时:
pacman -S mingw-w64-x86_64-protobuf
。 参考./configure
make
(如果 aclocal 等出现任何错误,请尝试运行pacman -S autoconf
,然后重试make
)make install
第 4 步:
就是这样。您现在应该能够使用 protobuf 编译您的项目。
例如,使用 protoc to cpp code & 编译 .proto 文件头文件:
protoc --cpp_out=$OUTDIR example.proto
pb.cc
&将生成一个pb.h
文件。g++ -I "C:\Path-To-Protobuf\protobuf-3.21.4 \src" "代码路径\writer.cpp" "代码路径\example.pb.cc" -o "代码路径\writer.exe" -L "C:\Path-To-Protobuf\protobuf-3.21.4\src\.libs" -lprotobuf -pthread
(我猜“-pthread”最后并不重要。)
注意(我的问题had): 例如 mingw 在系统环境变量列表中的路径顺序非常重要。
LATEST ANSWER
Here is an answer for the present-day versions of protobuf source distribution, for using protobuf for C++ language in Windows.
(My protobuf version is 21.4- libprotoc 3.21.4)
Referring to the answer by @peter-remmers
Step 0: Download protobuf zip file from release page. e.g. "protobuf-cpp-3.21.4.zip"
Step 1: Download & install msys2: https://www.msys2.org/
Make sure to to do these:
pacman -Syu
pacman -Syu
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
Step 2: Add the mingw bin files' path to system's environment variables.
g++ --version
in the terminal (e.g. Mine is 12.1.0)Step 3: Setting Up C++ runtime with protobuf libraries:
pacman -S mingw-w64-x86_64-protobuf
. Reference./configure
make
(In case of any error with aclocal, etc. Try runningpacman -S autoconf
, then try againmake
)make install
Step 4:
That's it. You should now be able to compile your project with protobuf.
e.g. To compile a .proto file using protoc to cpp code & header files:
protoc --cpp_out=$OUTDIR example.proto
pb.cc
& apb.h
file will be generated.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.)
NOTE (The problem I had): Order of paths of e.g. mingw in system's environment variables list matters very much.