Googletest Cmake

The text was updated successfully, but these errors were encountered. GoogleTest and CMake This is a quick recipe for setting up CMake to use googletest in your projects. First, make a tests folder in the root of your project. Then, add addsubdirectory (tests) to your CMakeLists.txt, after you’ve finished adding the libraries in your project. Googletest is available as a git repository which you can clone and then copy into your project. You could go all fancy with CMake scripts to download it if it's not already in your project, but since you can clone once and copy later on I choose not to automate it.

Googletest cmake add_test
  1. Cmake file for googlemock and googletest usage. GitHub Gist: instantly share code, notes, and snippets.
  2. Using CMake, googletest and Jenkins makes it very similar to unit testing in Java projects.

How to install googletest library?

Setting up googletest (or gtest, C++ unit testing library) can be tricky. When starting a new project, integrating googletest can be problematic, despite having no such issues with other libraries.

The following guide provides instructions on how to compile and add googletest to a C++ project. It uses pkg-config to obtain compiler flags and works with build systems such as cmake.

It was tested on Ubuntu but might work on other systems as well.

Download and compile googletest

Download the latest release from

and extract it:

After that, we can compile it:

Now, copy the headers to a directory where compilers can find them:

Then, copy static libraries:

Additionally, you can also add googlemock library (C++ framework for mocking objects):

Find googletest with pkg-config

After compiling and installing library files, try to find the library with pkg-config:

If you get a message about gtest not being found (“Package gtest was not found in the pkg-config search path”) then it means pkg-config cannot find gtest’s .pc file which provides the necessary configuration.

Run the following command to see the list of directories read by pkg-config:

Choose one of them, e.g. /usr/lib/pkgconfig, and inside that directory create a file named gtest.pc which should like similar to this:

Having done that, the following command:

should produce valid output, i.e. similar to

Integrating googletest with cmake

If you use cmake to build your program, you can use pkg-config to find correct compilation flags.

First, in order to add support for pkg-config in cmake, add the following in CMakeLists.txt:

Then, find gtest with pkg-config:

You can verify that it’s available by printing GTEST_FOUND variable

and running cmake . on your project.

After you confirm cmake can find the library, just add a definition for your test executable, e.g.

and then link gtest with it:

Running build command e.g. cmake . && make should now successfully build the executable.

That concludes googletest installation. See the next section if you need to add googlemock (C++ mocking library).

Integrating googlemock with a C++ project

First, ensure you have copied libgmock*.a libraries to /usr/local/lib, i.e. by executing:

Create gmock.pc configuration file in /usr/lib/pkgconfig so that pkg-config can resolve compiler flags:

Then, find gmock in CMakeLists.txt:

Finally, link your tests with googlemock by adding ${GMOCK_LIBRARIES} variable:

After all those steps, tests using googletest/googlemock should compile correctly by executing standard commands (cmake . && make)!

Related posts

Skip to main content

C++ project setup with CMake & unit tests (google test)

Published: 01-10-2019 Last update: 06-11-2019 Author: Remy van Elst Text only version of this article

❗ This post is over one year old. It may no longer be up to date. Opinions may have changed.

Table of Contents

  • CMakeLists.txt
  • Add some (example) source code and tests
How to embed googletest in projects

This guide will show you how to setup a new C++ project with CMake and unit testsvia Google's test framework. With this setup you can get started right away with test-driven-development in C++. It is also simple enough to look and figureout how to add gtest to your existing project and start doing TDD on your legacy (existing) codebase.

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.
You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

The picture below shows the end result, a running unit test:

There are a million different ways to 'do' C++ projects, but using CMake andthe google testing framework has my preference. That's not to say that using a Makefile or Boost Unit Test is bad, use whatever suits your needs. This guide however will focus on just CMake and gtest.

It assumes a system running Ubuntu (18.04). It also works on Windows with mingw,but I haven't tested in with MSVC.

My preferred code editor is CLion from Jetbrains, which has most of this built in. This guide however focusses on the manual / command line way since CLion isnonfree (and paid) software.

The process is not that complicated:

  • Install software (cmake and googletest)
  • Create folder structure
  • Create the CMakeLists.txt files
  • Create some sample code and sample tests
  • Compile everything
  • Run the tests

Install cmake & googletest

Googletest Cmake Tutorial

I assume you already have your compiler installed and working. Installing cmakecan be done with the package manager on Ubuntu:

On Windows, you can use MinGW or cygwin to install your development tools including CMake. Clion offers a nice GUI for that.

Googletest is available as a git repository which you can clone and then copyinto your project. You could go all fancy with CMake scripts to download itif it's not already in your project, but since you can clone once and copy later on I choose not to automate it. Clone the repository:

gtest comes with a CMakeLists.txt so integrating it in your project is easy.

Folder structure

Create your C++ project folder. I like to keep the following structure for simpleprojects:

Here is a oneliner to create the folders:

Copy the googletest repository folder your cloned earlier into the lib/ folder.

If you have multiple components you can create extra sub folders, but that doesrequire tweaking the CMakeLists.txt files to work with multiple libraries.

Most of my personal projects are simple enough to fit into one folder as above.

In the tst folder the unit tests reside. I try to keep the tests limited to the same function in seperate files. In the above example I have Formula.h and Formula.cpp, which house the example Formula class. All unit tests relatedto this class thus should reside in Formula-test.cpp.


The file CMakeLists.txt contains a set of directives and instructionsdescribing the project's source files and targets (executable, library, orboth). This can get quite complex quite fast, CMake has many options. I try tokeep it simple in this guide.

I'm using a non-recommended way to include files. For simple projects with a few files you should use the following:

I'm using this:

That is a recursive search to include all *.cpp and *.h in the folder. Inmy IDE I have auto-reload enabled, that way I can't forget to add a fileto CMakeLists.txt every time. For proper administration you should not use thissince it just includes everything, could have unwanted side-effects.

Update 2019-11-07: If you want Boost in this setup, read this article from me.

Each subdirectory in our case also needs a CMakeLists.txt file.

Googletest Cmake Deprecation Warning

Main folder CMakeLists.txt

The name of the project is ExampleProject, that variable is used in other files.The rest of the file just includes the different subfolders. If you omit the include_directories(src), your tests will not be able to find the header files.

src folder CMakeLists.txt:

The name of the compiled program will be ExampleProject_run, which is whatwe defined in add_executable. The add_library is used to include the codein the unit tests.

tst folder CMakeLists.txt:

This list used the src defined library and adds the tests as a target. The compiled executable file is named ExampleProject_tst.

Add some (example) source code and tests

At this point you start developing. But since this is a example setup, I'll adda simple class file to show you how to do the unit tests.

Source Code

Copy the below code into your project:

Cmake Run Tests




This function returns the given int multiplied by 2.

Test code

The following code is to setup the unit tests.


Googletest Cmake Tutorial

This file will run all the tests and since we recursively included everythingwith CMake, it will effectively run all tests in all files in this folder.


The Google Test Primer is a great starting point to learn more on the specificsof the testing framework.

Compile all the things

Now that we have sourcecode and testcode in place we can compile everything (both the binary and the tests).

Do note that you should do this in the build folder. If you do it in the main folder it will work, but it will litter up the directory.


There are now a bunch of files and folders in the build folder, most important,the Makefile. You can now compile the project:


You now have two executable files, as defined in the CMakeLists.txt:

Run all the things

If all went well, the code should run:


The tests as well:


A quick one-liner to compile and run the tests. You can run this wheneveryou want to re-run the tests (after changing code for example):

Googletest Cmake Windows


As you can see I changed a unit test so that it failed.

Tags: boost, c++, cmake, cpp, development, googletest, linux, software, testing, ttd, tutorials