如何使用 mingw 和 msys 在 Windows 上编译 googletest?
我的需求很简单。我必须使用 MinGW 和 msys 在 Windows 上编译和使用 googletest。有人有这样做的经验吗?
感谢您的回答。
My need is simple. I have to compile and use googletest on windows using MinGW with msys. Has anyone some experience doing this?
Thanks for answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我花了一些时间,但我想通了。这是为遇到同样问题的人提供的指南。
为了能够在 Windows 上编译 GoogleTest,请按照以下说明进行操作:
我假设您已安装了 MSYS 的 MinGW。
从官方网站http://www.cmake.org/下载并安装CMake。使用 Win32 安装程序
版本。完成安装过程后,从以下位置复制可执行文件
将“xxx/CMake/bin”修改为“xxx/MinWG/bin”。
从 http://www.python.org/ 下载并安装 Python。同样,Windows 安装程序完成了这项工作
美好的。
完成安装过程后,复制“python.exe”
将 python 文件夹形成为
“xxx/MinWG/bin”。
从 http://code.google.com/p/googletest/ 下载最新的稳定版 GoogleTest 并解压将其放入某个文件夹中。
运行MSYS终端并执行以下命令。
<前><代码>cd xxx/gtest-xxx
cmake -G “MSYS Makefiles”
制作
如果您在 pthread 中遇到编译错误,请按照这些说明操作。
将包含文件夹“xxx/gtest-xxx/include”复制到您的 MinGW gcc 包含中。
将库文件“xxx/gtest-xxx/*.a”复制到您的 MinGW gcc 库中。
当您编译测试时,向 gcc 添加“-lgtest”参数。
编辑
评论家是对的。可执行文件的处理对我有用,但通常这不是一个好的做法。尝试改用符号链接。
It took me some time but I figured it out. Here is the guide for anyone who face the same problem.
To be able to compile GoogleTest on Windows follow this instructions:
I assume you have MinGW with MSYS istalled.
Download and install CMake from the official site http://www.cmake.org/. Use the Win32 installer
version. Once you have completed the installation process copy executable files from
"xxx/CMake/bin" to "xxx/MinWG/bin".
Download and install Python from http://www.python.org/. Again, the Windows installer does the job
fine.
Once you have completed the installation process copy the "python.exe"
form python folder to
"xxx/MinWG/bin".
Download the latest stable GoogleTest from http://code.google.com/p/googletest/ and unpack it into some folder.
Run MSYS terminal and execute following commands.
If you have compilation errors from pthread follow these instructions.
Copy the include folder "xxx/gtest-x.x.x/include" into your MinGW gcc include.
Copy the library files "xxx/gtest-x.x.x/*.a" into your MinGW gcc lib.
When you compile tests add "-lgtest" parameter to gcc.
EDIT
Commentators are right. The coping of executables worked for me but generaly it is not a good practice. Try to use a symbolic link instead.
要在不使用 cmake/python 而仅使用 mingw make 的情况下构建 libgtest.a,gtest 现在有一个“make”文件夹,其中包含一个普通的旧 makefile。
创建的库是一个完整的静态库,没有生成 dll。
这应该就是全部了。
顺便说一句,这也适用于构建 googlemock,只需进入 googlemock 文件夹而不是 googletest,然后按照相同的步骤操作即可。
To build libgtest.a without cmake/python, but only using mingw make, gtest now has a 'make' folder with a plain old makefile in it.
The library created is a full static library with no dll's generated.
That should be all.
By the way, this also works for building googlemock, just enter the googlemock folder instead of googletest, and follow the same procedure.
这个问题是在2011年提出的,得票最多的答案也在同年得到了回答。因此,新的答案将提高问题的有效性。
您需要的工具和工具我测试了:
Mingw64 8.0。 2
GoogleTest GitHUb 源代码存储库分支 1.10.0
CMake 3.20.4
和 Windows10
步骤
双击安装 mingw64,并选择对应的路径
目录名称之间没有空格,例如“Program Files”
打开Windows设置,然后搜索环境变量
然后打开对话框来编辑 Path 环境变量
在Windows Path环境中添加mingw64/bin目录名
变量例如C:\Users[USERNAME]\mingw64\bin(替换[USERNAME]
使用您的用户名,例如 Michael 或 Lee 等)
安装 CMake。这是双击安装过程。确保,其
bin 目录路径添加到 Path 环境变量中。
它将安装在 C:/Program Files/...
下载 GoogleTest 存储库,将其解压并创建一个构建目录
在提取的目录中。
执行以下命令
$ cd 构建
$ cmake .. -G "MinGW Makefiles"
$ mingw32-make.exe
从构建目录复制四个静态库(*.a)
进入 MingW64 的 lib
复制整个 gtest 目录并复制到该文件夹
你已经完成了。您可以构建 Googltest 并将其与您的 C++ 项目链接。我还粘贴了一个 CMakelists.txt 示例,
希望它能起作用。
The question was asked in 2011 and answer with most votes is also answered the same year. So, a fresh answer would improve the question effectiveness.
Tools You need & I tested with:
Mingw64 8.0.2
GoogleTest GitHUb Source Code repo branch 1.10.0
CMake 3.20.4
and Windows10
Steps
Install the mingw64 by double clicking and chose the path which does
not have space between directory names e.g. "Program Files"
Open settings of the windows and then search environment variables
and oepn the dialog box to edit the Path environment variable
Add the mingw64/bin directory name in the Windows Path Environment
variable e.g. C:\Users[USERNAME]\mingw64\bin (replace [USERNAME]
with your username e.g. Michael or Lee etc)
Install CMake. It is double click install procedure. Make sure, its
bin directory path is added in the Path Environment Variable.
It would be installed in C:/Program Files/...
Download GoogleTest repo extract it and create a build directory
inside the extracted directory.
Execute the following commands
$ cd build
$ cmake .. -G "MinGW Makefiles"
$ mingw32-make.exe
Copy the four static libraries(*.a) from build directory
into lib of MingW64
Copy that whole gtest directory and copy to the folder
You are done to go. You can build and link Googltest with your C++ project. I also paste a CMakelists.txt sample
I hope it would work.
来自 https://github.com/google/googletest/tree/master/ 的自述文件谷歌测试
:
将 Google Test 构建为独立项目时,典型的工作流程开始
with:
如果你想构建Google Test的样本,你应该替换最后一个命令
和
From the README of https://github.com/google/googletest/tree/master/googletest
:
When building Google Test as a standalone project, the typical workflow starts
with:
If you want to build Google Test's samples, you should replace the last command
with
作为替代方案,也可以使用通常的 MSYS/Mingw make 构建 googletest。
所以这是我的替代方法:
确保 MSys/MingW 已安装在您的 Windows 上,并且 PATH 环境已设置为它
打开一个 cmd 窗口 - 您也可以在此处显式设置 PATH
CD 到解压后的 googletest 目录
使用 sh (MSys 的一部分)调用配置:
sh configure
make
->应该构建libgtest.a
。它位于您的 googletest-directorylib/.libs
子目录请参阅 googletest 的 README,了解如何将
libgtest.a
集成到您的系统。另请参阅 googletest wiki 中的 googletest 入门了解如何编译。或者指定 gcc-L/lib/.libs
的库路径并添加-lgtest
以链接到您的测试项目可执行文件。当使用
ASSERT_DEATH
宏检查测试代码中的断言(意味着您的库或应用程序中的断言,而不是 googletest 中的断言)时,请调用SetErrorMode
- 示例 main:< /p>As alternative it is also possible to build googletest using the usual MSYS/Mingw make.
So here is my alternative way:
Make sure MSys/MingW is installed on your Windows and the PATH environment is set to it
Open a cmd window - you can set the PATH explicitly here as well
CD to the unzipped googletest directory
Call configure with sh (part of MSys):
sh configure
Call
make
->libgtest.a
should be built. It is placed in your googletest-directorylib/.libs
subdirectorySee README of googletest of how to integrate the
libgtest.a
to your system. Also see googletest primer in the googletest wiki of how to compile. Alternatively specify the library path for gcc-L<googleTestDir>/lib/.libs
and add-lgtest
to link with your test project executable.When using
ASSERT_DEATH
macro to check for asserts in your tested code (meaning asserts in your lib or application, not in googletest), callSetErrorMode
- example main:只要路径中有二进制文件,您就不需要复制它们。安装 python 和 CMake。在您的 msys(MinGW 控制台)中测试它们
,如果您看到路径,则您拥有二进制文件。如果没有,请将它们的路径添加到您的环境变量>PATH 中,或者仅在 msys 中更新(如有必要,更新安装路径)
然后您可以按照建议进行构建:
测试是否一切正常:
You don't need to copy the binaries as long as you have them in your path. Install python and CMake. Test them in your msys (MinGW console)
If you see the path, then you have the binaries. If not, add their path to your Environmental Variables>PATH or just update within msys (update installation paths if necessary)
Then you can build as suggested:
Test if everything works:
使用 MSYS2,只需安装 mingw-w64-x86_64-gtest 软件包:
然后使用标志
-lgtest -lgtest_main
编译测试。With MSYS2, simply install the mingw-w64-x86_64-gtest package:
Then compile tests with the flags
-lgtest -lgtest_main
.