如何使用 mingw 和 msys 在 Windows 上编译 googletest?

发布于 2024-10-20 22:55:46 字数 89 浏览 6 评论 0原文

我的需求很简单。我必须使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浅唱々樱花落 2024-10-27 22:55:46

我花了一些时间,但我想通了。这是为遇到同样问题的人提供的指南。

为了能够在 Windows 上编译 GoogleTest,请按照以下说明进行操作:

  1. 我假设您已安装了 MSYS 的 MinGW。

  2. 从官方网站http://www.cmake.org/下载并安装CMake。使用 Win32 安装程序
    版本。完成安装过程后,从以下位置复制可执行文件
    将“xxx/CMake/bin”修改为“xxx/MinWG/bin”。

  3. http://www.python.org/ 下载并安装 Python。同样,Windows 安装程序完成了这项工作
    美好的。
    完成安装过程后,复制“python.exe”
    将 python 文件夹形成为
    “xxx/MinWG/bin”。

  4. http://code.google.com/p/googletest/ 下载最新的稳定版 GoogleTest 并解压将其放入某个文件夹中。

  5. 运行MSYS终端并执行以下命令。

    <前><代码>cd xxx/gtest-xxx
    cmake -G “MSYS Makefiles”
    制作

  6. 如果您在 pthread 中遇到编译错误,请按照这些说明操作。

  7. 将包含文件夹“xxx/gtest-xxx/include”复制到您的 MinGW gcc 包含中。
    将库文件“xxx/gtest-xxx/*.a”复制到您的 MinGW gcc 库中。

  8. 当您编译测试时,向 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:

  1. I assume you have MinGW with MSYS istalled.

  2. 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".

  3. 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".

  4. Download the latest stable GoogleTest from http://code.google.com/p/googletest/ and unpack it into some folder.

  5. Run MSYS terminal and execute following commands.

    cd xxx/gtest-x.x.x
    cmake -G "MSYS Makefiles"
    make
    
  6. If you have compilation errors from pthread follow these instructions.

  7. 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.

  8. 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.

朱染 2024-10-27 22:55:46

要在不使用 cmake/python 而仅使用 mingw make 的情况下构建 libgtest.a,gtest 现在有一个“make”文件夹,其中包含一个普通的旧 makefile。

  1. 确保 mingw\bin 在路径中(尝试运行“g++”或其他命令)。
  2. 进入 gtest 'googletest\make' 文件夹并运行 'make'。
  3. 要进行测试,请运行“sample1_unittest”(应出现 gtest 示例测试输出)。
  4. 要生成库“libgtest.a”,请运行“ar -rv libgtest.a gtest-all.o”。

创建的库是一个完整的静态库,没有生成 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.

  1. Make sure, mingw\bin is in the path (try running 'g++' or something).
  2. Enter the gtest 'googletest\make' folder and run 'make'.
  3. To test, run 'sample1_unittest' (gtest sample test output should appear).
  4. To generate the library 'libgtest.a', run 'ar -rv libgtest.a gtest-all.o'

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.

江心雾 2024-10-27 22:55:46

这个问题是在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)

[例如:C:\Users[用户名]\sourcecodes\googletest-master\build\lib]

进入 MingW64 的 lib

[例如:C:\Users[用户名]\mingw64\x86_64-w64-mingw32\lib]

  • 转到 GoogleTest 提取的存储库,导航到

[例如
C:\Users[用户名]\sourcecodes\googletest-master\googletest\include\gtest]

复制整个 gtest 目录并复制到该文件夹

C:\Users[用户名]\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include

你已经完成了。您可以构建 Googltest 并将其与您的 C++ 项目链接。我还粘贴了一个 CMakelists.txt 示例,

cmake_minimum_required(VERSION 3.12)

project(ProjectName VERSION 1.0.0 LANGUAGES CXX)
    
include_directories(include)

set(SOURCES src/library.cpp include/library.h)
add_executable(libabc ${SOURCES})

#############
## Testing ##
#############

enable_testing()
find_library(GTest gtest)

add_executable (unitTest test/unit_test.cpp)
target_link_libraries (unitTest gtest gtest_main)
add_test(AllFactTest unitTest)

希望它能起作用。

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

[ex: C:\Users[USERNAME]\sourcecodes\googletest-master\build\lib]

into lib of MingW64

[ex: C:\Users[USERNAME]\mingw64\x86_64-w64-mingw32\lib]

  • Go to the GoogleTest extracted repo, navigate to

[ex
C:\Users[USERNAME]\sourcecodes\googletest-master\googletest\include\gtest]

Copy that whole gtest directory and copy to the folder

C:\Users[USERNAME]\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include

You are done to go. You can build and link Googltest with your C++ project. I also paste a CMakelists.txt sample

cmake_minimum_required(VERSION 3.12)

project(ProjectName VERSION 1.0.0 LANGUAGES CXX)
    
include_directories(include)

set(SOURCES src/library.cpp include/library.h)
add_executable(libabc ${SOURCES})

#############
## Testing ##
#############

enable_testing()
find_library(GTest gtest)

add_executable (unitTest test/unit_test.cpp)
target_link_libraries (unitTest gtest gtest_main)
add_test(AllFactTest unitTest)

I hope it would work.

友欢 2024-10-27 22:55:46

来自 https://github.com/google/googletest/tree/master/ 的自述文件谷歌测试
:
将 Google Test 构建为独立项目时,典型的工作流程开始
with:

mkdir mybuild       # Create a directory to hold the build output.
cd mybuild
cmake ${GTEST_DIR}  # Generate native build scripts.

如果你想构建Google Test的样本,你应该替换最后一个命令

cmake -Dgtest_build_samples=ON ${GTEST_DIR}

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:

mkdir mybuild       # Create a directory to hold the build output.
cd mybuild
cmake ${GTEST_DIR}  # Generate native build scripts.

If you want to build Google Test's samples, you should replace the last command
with

cmake -Dgtest_build_samples=ON ${GTEST_DIR}
平安喜乐 2024-10-27 22:55:46

作为替代方案,也可以使用通常的 MSYS/Mingw make 构建 googletest。

所以这是我的替代方法:

  1. 确保 MSys/MingW 已安装在您的 Windows 上,并且 PATH 环境已设置为它

  2. 打开一个 cmd 窗口 - 您也可以在此处显式设置 PATH

  3. CD 到解压后的 googletest 目录

  4. 使用 sh (MSys 的一部分)调用配置:sh configure

  5. < p>调用make ->应该构建 libgtest.a 。它位于您的 googletest-directory lib/.libs 子目录

  6. 请参阅 googletest 的 README,了解如何将 libgtest.a 集成到您的系统。另请参阅 googletest wiki 中的 googletest 入门了解如何编译。或者指定 gcc -L/lib/.libs 的库路径并添加 -lgtest 以链接到您的测试项目可执行文件。

  7. 当使用 ASSERT_DEATH 宏检查测试代码中的断言(意味着您的库或应用程序中的断言,而不是 googletest 中的断言)时,请调用 SetErrorMode - 示例 main:< /p>

    #include ;
    #include“gtest/gtest.h”
    
    int main (int argc, char** argv)
    {
        // 这可以防止弹出烦人的错误消息框
        // 当你的程序代码中调用assert时
        SetErrorMode(SEM_NOGPFAULTERRORBOX);
        ::测试::InitGoogleTest(&argc, argv);
        返回 RUN_ALL_TESTS();
    }
    

As alternative it is also possible to build googletest using the usual MSYS/Mingw make.

So here is my alternative way:

  1. Make sure MSys/MingW is installed on your Windows and the PATH environment is set to it

  2. Open a cmd window - you can set the PATH explicitly here as well

  3. CD to the unzipped googletest directory

  4. Call configure with sh (part of MSys): sh configure

  5. Call make -> libgtest.a should be built. It is placed in your googletest-directory lib/.libs subdirectory

  6. See 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.

  7. When using ASSERT_DEATH macro to check for asserts in your tested code (meaning asserts in your lib or application, not in googletest), call SetErrorMode - example main:

    #include <windows.h>
    #include "gtest/gtest.h"
    
    int main (int argc, char** argv)
    {
        // this prevents annoying error message boxes popping up
        // when assert is called in your program code
        SetErrorMode(SEM_NOGPFAULTERRORBOX);
        ::testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }
    
巴黎盛开的樱花 2024-10-27 22:55:46

只要路径中有二进制文件,您就不需要复制它们。安装 python 和 CMake。在您的 msys(MinGW 控制台)中测试它们

which cmake
which python

,如果您看到路径,则您拥有二进制文件。如果没有,请将它们的路径添加到您的环境变量>PATH 中,或者仅在 msys 中更新(如有必要,更新安装路径)

export PATH=$PATH:/c/Program Files (x86)/CMake/bin/cmake.exe:/c/Python27/python.exe

然后您可以按照建议进行构建:

cd xxx/gtest-x.x.x
cmake -G "MSYS Makefiles"
make

测试是否一切正常:

cd make
make
./sample1_unittest.exe

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)

which cmake
which python

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)

export PATH=$PATH:/c/Program Files (x86)/CMake/bin/cmake.exe:/c/Python27/python.exe

Then you can build as suggested:

cd xxx/gtest-x.x.x
cmake -G "MSYS Makefiles"
make

Test if everything works:

cd make
make
./sample1_unittest.exe
肤浅与狂妄 2024-10-27 22:55:46

使用 MSYS2,只需安装 mingw-w64-x86_64-gtest 软件包:

pacman -S mingw-w64-x86_64-gtest

然后使用标志 -lgtest -lgtest_main 编译测试。

With MSYS2, simply install the mingw-w64-x86_64-gtest package:

pacman -S mingw-w64-x86_64-gtest

Then compile tests with the flags -lgtest -lgtest_main.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文