C Cmake

This tutorial aims to get you up and running with GoogleTest using CMake. Ifyou’re using GoogleTest for the first time or need a refresher, we recommendthis tutorial as a starting point. If your project uses Bazel, see theQuickstart for Bazel instead.


What is CMake CMake is a cross-platform open-source tool for defining build processes that run across multiple platforms by abstracting away native build environments and compilers. CPM.cmake should be invisible to library users as it's a self-contained CMake Script. If problems do arise, users can always opt-out by defining the CMake or env variable CPMUSELOCALPACKAGES, which will override all calls to CPMAddPackage with the according findpackage call.

To complete this tutorial, you’ll need:

  • A compatible operating system (e.g. Linux, macOS, Windows).
  • A compatible C++ compiler that supports at least C++11.
  • CMake and a compatible build tool for building theproject.
    • Compatible build tools includeMake,Ninja, and others - seeCMake Generatorsfor more information.

See Supported Platforms for more information about platformscompatible with GoogleTest.

If you don’t already have CMake installed, see theCMake installation guide.

Note: The terminal commands in this tutorial show a Unix shell prompt, but thecommands work on the Windows command line as well.

C cmake include path

Set up a project

CMake uses a file named CMakeLists.txt to configure the build system for aproject. You’ll use this file to set up your project and declare a dependency onGoogleTest.

First, create a directory for your project:

Next, you’ll create the CMakeLists.txt file and declare a dependency onGoogleTest. There are many ways to express dependencies in the CMake ecosystem;in this quickstart, you’ll use theFetchContent CMake module.To do this, in your project directory (my_project), create a file namedCMakeLists.txt with the following contents:

The above configuration declares a dependency on GoogleTest which is downloadedfrom GitHub. In the above example, 609281088cfefc76f9d0ce82e1ff6c30cc3591e5 isthe Git commit hash of the GoogleTest version to use; we recommend updating thehash often to point to the latest version.



For more information about how to create CMakeLists.txt files, see theCMake Tutorial.

Create and run a binary

Cmake Source Directory


With GoogleTest declared as a dependency, you can use GoogleTest code withinyour own project.

As an example, create a file named hello_test.cc in your my_projectdirectory with the following contents:

GoogleTest provides assertions that you use to test thebehavior of your code. The above sample includes the main GoogleTest header fileand demonstrates some basic assertions.

To build the code, add the following to the end of your CMakeLists.txt file:

The above configuration enables testing in CMake, declares the C++ test binaryyou want to build (hello_test), and links it to GoogleTest (gtest_main). Thelast two lines enable CMake’s test runner to discover the tests included in thebinary, using theGoogleTest CMake module.

Now you can build and run your test:

Congratulations! You’ve successfully built and run a test binary usingGoogleTest.

Next steps

C++ Cmake Tools For Windows

  • Check out the Primer to start learning how to write simpletests.
  • See the code samples for more examples showing how to use avariety of GoogleTest features.