use MinGW compile googletest on windows

table of contents

enviornments

brief description of software installation

I‘ll not write installation steps in detials, because there are many relaed article on the internet. It just some key points of I think.

MinGW installation

use MinGW compile googletest on windows
The figure shows my MinGW installation configuration and it looks work as well, I’m not sure other config information can work or not.

cmake installation

use MinGW compile googletest on windows
Make sure you choose “Add Cmake to the system PATH for all users” or “Add Cmake to the system PATH for the current user”, otherwise, you need manually add the system PATH.

googletest

The project of googletest is on github.com, I believe you already have git bash software. We need to clone this project code from github. In a moment, we’ll use the git command.So we should use clone command instead of downloading directly.

git clone https://github.com/google/googletest.git

compile googletest

Firstly, enter the googletest direction and open a git bash prompt windows, Run the following command in sequence.

git checkout release-1.7.0
cmake -G "MinGW Makefiles" -D CMAKE_C_COMPILER=gcc.exe ^
      -D CMAKE_CXX_COMPILER=g++.exe -DCMAKE_CXX_FLAGS=-std=c++11 ^
      -D CMAKE_MAKE_PROGRAM=mingw32-make.exe
make

now, if everything goes well, we can find libgtest.a and ** libgtest_main.a** in googletest direction.It is a very good info.
use MinGW compile googletest on windows

attendion : git ckeckout release-1.7.0is necessery. I tired to compile with the main branch, but it failed.By querying related content, someone recommends using it.
use MinGW compile googletest on windows
Many arcticles indcatie suggest use cmake -G "MinGW Makefiles" -DCMAKE_CXX_FLAGS=-std=c++11,Unfortunately, I failed again.

a simple test

copy libgteat.a, libgteat_main.a and $(googletest)\include\gtest folder to project direction. New a test.cpp file.
code,

#include <iostream>
#include "gtest/gtest.h"

int foo()
{
    return 0;
}

TEST(FooTest, test)
{
    EXPECT_EQ(0, foo());
}

int main(int argc, char *argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

complie this project and run it,

g++ test.cpp  -L. -lgtest -lgtest_main -lpthread -I. -o test.exe
./test.exe

use MinGW compile googletest on windows
In this steps, I have a problem again. The pthread lib must be after gtest and gtest_main, I don’t know before and I didn’t pay attention to the order.I don’t know why, if you know, please leave a message and let me know.thank you very much.

上一篇:win11下,QT6+Opencv配置


下一篇:Windows11下VSCode的C/C++环境搭建与配置