C++ - Google Test - Using a Makefile to generate tests

After succeeded in accomplishing the Google Test setup by hand we are going to see how to use a Makefile to create our running tests.

Of course, this time, we will use the Cygwin CLI in order to create our tests example.

So don't wait anymore, let's get started.

First of all

For this Google Test tutorial, we need some useful tools to create an example:

  • Google Test
  • Cygwin

Google Test

Of course, we need the Google Test framework.

We are going to use the 1.8.0 version.

To know exactly which version you use, I recommend using the Google Test official web page from GitHub and pick the version you want (in our case the 1.8.0 release):

For our tutorial I choose to install Google Test in the following directory:

  • C:\dev\c++\mylib\googletest-release-1.8.0


Cygwin will help us to execute the Google Test example within a Makefile:

If you don't know what to install, just follow this Cygwin tutorial I already did in the past (Part 1 and 2).

OK we have now all tools needed.

It's time to begin this tutorial.


We are going to use the Makefile with Cygwin.

This Makefile is in the following directory:

  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest\make

It's not so complex but it needs some explanations even if there are a lot of comments within.

gtest-all.o rule

The rule is quite explicit, for example with this snippet:

gtest-all.o : $(GTEST_SRCS_)

We have here the getest-all.o rule that needs to check if  the dependencies $(GTEST_SRCS_) have changed since the last compilation.

And if yes, it will compile the gtest-all.cc file.

In this case: $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

So globally all .cc and .h files within the following directories:

  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest\src
  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest\include

We can also note the $(CXX) which is the compiler g++ and $(CPPFLAGS) which are not flags variable for C++ but for the C PreProcessor.

In this particular situation the Makefile calls the -isystem option with the parameter $(GTEST_DIR)/include path.

Indeed, as we are creating a library, we have to use the -isystem option in order to tell preprocessor that we want to put those includes in the system header search path.

gtest_main.o rule

Then we have the getest_main.o rule which is quite the same as the gtest-all.o except that we will compile the gtest_main.cc file.

gtest.a rule

With the gtest.a rule, we have here a library to generate if the dependencies gtest-all.o is ready.

If yes, it will create a static libray with the GNU ar program, where ar stands for archive.

And the $(ARFLAGS) implicit variable contains the -rv option to the ar tool.

The -r option inserts the files (members) passed as arguments directly into the archive (deleting any existing members if their names match those being added).

The -v option will display a line of output for each file inserted (it's a verbose option).

And finally the $@ and $^ automatic variables which refer respectively to:

  • $@ --> gtest.a (the rule)
  • $^ --> gtest-all.o (the dependencies)

Those automatic variables are just sort of variables that the GNU make utility can read and interpret within a Makefile.

Other rules are the same, so easy to decrypt.

Let's make

It's now time to use this Makefile.

But just before, as Google makes great things, they added some examples to test the Google Test library.

You can find these tests in the following directory:

  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest\samples

The sample1_unittest .cc is already in the Makefile ready to be compiled with the Google Test library.

So let's try it.

In your Cygwin prompt type the pwd command:


You should see:


If you don't have it, please come to this directory by typing:

cd /cygdrive/c/dev/c++/mylib/googletest-release-1.8.0/googletest/make 

Now let's play with the Makefile by typing:


You should see something like that appear:

> make
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1.cc
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1_unittest.cc
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
echo "CXX = " g++
CXX =  g++
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
ar rv gtest_main.a gtest-all.o gtest_main.o
ar: creating gtest_main.a
a - gtest-all.o
a - gtest_main.o
g++ -isystem ../include -g -Wall -Wextra -pthread -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest

You can check that the libtest.a has been created in:

  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest

And launch the executable generated (still in the /make directory):


This time all tests should be run:

> ./sample1_unittest.exe
Running main() from gtest_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (1 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (1 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (4 ms total)
[  PASSED  ] 6 tests.


It wasn't such a big deal to use the Google Test Makefile.

A nice example and some explanations, you are ready to go now.

Congratulations if you read the tutorial to here, once again good job, you did it. cool


Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.