How to compile googletest on windows using mingw with msys?

你。 提交于 2019-12-30 00:05:32

问题


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.


回答1:


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.




回答2:


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.




回答3:


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();
    }
    



回答4:


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


来源:https://stackoverflow.com/questions/5248237/how-to-compile-googletest-on-windows-using-mingw-with-msys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!