C++ 如何使用 mingw 和 msys 在 windows 上编译 googletest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5248237/
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-28 17:45:10  来源:igfitidea点击:

How to compile googletest on windows using mingw with msys?

c++windowsmingwgoogletest

提问by Rusty Horse

My need is simple. I have to compile and use googletest on windows using MinGW with msys. Has anyone some experience doing this?

我的需求很简单。我必须在 Windows 上使用带有 msys 的 MinGW 编译和使用 googletest。有没有人有这样做的经验?

Thanks for answers.

感谢您的回答。

回答by Rusty Horse

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:

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

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

  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.

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

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

  3. http://www.python.org/下载并安装 Python 。同样,Windows 安装程序可以很好地完成工作。完成安装过程后,将 python 文件夹中的“python.exe”复制到“xxx/MinWG/bin”。

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

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

    cd xxx/gtest-x.x.x
    cmake -G "MSYS Makefiles"
    make
    
  6. 如果您有来自 pthread 的编译错误,请按照以下说明进行操作。

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

  8. 当您编译测试时,将“-lgtest”参数添加到 gcc。

EDITCommentators are right. The coping of executables worked for me but generaly it is not a good practice. Try to use a symbolic linkinstead.

编辑评论员是对的。处理可执行文件对我有用,但一般来说这不是一个好习惯。尝试改用符号链接

回答by Jesper Matthiesen

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.

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

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

The library created is a full static library with no dll's generated.

创建的库是一个完整的静态库,没有生成 dll。

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.

顺便说一句,这也适用于构建 googlemock,只需输入 googlemock 文件夹而不是 googletest,并按照相同的步骤操作。

回答by michael_s

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

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

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.ashould be built. It is placed in your googletest-directory lib/.libssubdirectory

  6. See README of googletest of how to integrate the libgtest.ato your system. Also see googletest primer in the googletest wikiof how to compile. Alternatively specify the library path for gcc -L<googleTestDir>/lib/.libsand add -lgtestto link with your test project executable.

  7. When using ASSERT_DEATHmacro 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();
    }
    
  1. 确保您的 Windows 上安装了 MSys/MingW 并且设置了 PATH 环境

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

  3. CD 到解压后的 googletest 目录

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

  5. 呼叫make->libgtest.a应该建立。它位于您的 googletest-directorylib/.libs子目录中

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

  7. 当使用ASSERT_DEATH宏检查测试代码中的断言时(意思是在你的库或应用程序中,而不是在 googletest 中),调用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();
    }
    

回答by Halil Kaskavalci

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)

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

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)

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

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