简体   繁体   中英

How to compile googletest on windows using mingw with msys?

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.

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-xxx cmake -G "MSYS Makefiles" make
  6. If you have compilation errors from pthread follow these instructions.

  7. Copy the include folder "xxx/gtest-xxx/include" into your MinGW gcc include. Copy the library files "xxx/gtest-xxx/*.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.

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.

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}

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

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

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 eg "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 eg C:\\Users[USERNAME]\\mingw64\\bin (replace [USERNAME] with your username eg 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM