How to compile googletest on windows using mingw with msys?
Asked Answered
K

7

15

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.

Kimbell answered 9/3, 2011 at 15:45 Comment(2)
Please describe what is going wrong.Honewort
I have make some progress. It seems it can not install without a python which I do not have. I am tying to solve it now.Kimbell
K
16

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.

Kimbell answered 12/3, 2011 at 12:37 Comment(3)
Why copy and not symlink those executables?Microsecond
Actually both CMake and Python aren't build statically, so both of them don't work (crash and infinite loop) when called from msys shell.Microsecond
"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." It seems not work for me.Could you please show the full path for me?Ordnance
U
3

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.

Urbanite answered 18/10, 2016 at 15:15 Comment(2)
thanks @jesper-matthiesen it worked. Incase if someone faces error of ld.exe couldn't find lpthread. Try running mingw-get install libpthreadgc-dev as mentioned in comment of link. ThanksPirog
I can't see there is a folder named as googletest\make indeed.Ordnance
B
3

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.

Booster answered 23/6, 2021 at 19:55 Comment(1)
This helped me. Just don't forget to add target_link_libraries (unitTest gtest gtest_main) like I did. Otherwise the compiler wont find the binaries.Spirillum
N
2

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}
Nopar answered 9/1, 2021 at 9:52 Comment(0)
M
1

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();
    }
    
Merchandising answered 17/6, 2014 at 8:53 Comment(0)
P
1

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
Pugilism answered 27/2, 2015 at 13:50 Comment(0)
M
0

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.

Mcmillian answered 29/1, 2023 at 2:47 Comment(1)
I did install teh mingw-w64-x86_64-gtest packge. Then I was getting this strange error child_copy: cygheap read copy failed, 0x800000000..0x8000102D8, done 0, windows pid 19780, Win32 error 6. The error only went away once I downloaded GoogleTest code and compiled it. It would have been very convenient to use the pre-built MSYS2 package, but there is some build difference.Stature

© 2022 - 2024 — McMap. All rights reserved.