xref: /freebsd-src/contrib/googletest/docs/quickstart-cmake.md (revision 28f6c2f292806bf31230a959bc4b19d7081669a7)
1*28f6c2f2SEnji Cooper# Quickstart: Building with CMake
2*28f6c2f2SEnji Cooper
3*28f6c2f2SEnji CooperThis tutorial aims to get you up and running with GoogleTest using CMake. If
4*28f6c2f2SEnji Cooperyou're using GoogleTest for the first time or need a refresher, we recommend
5*28f6c2f2SEnji Cooperthis tutorial as a starting point. If your project uses Bazel, see the
6*28f6c2f2SEnji Cooper[Quickstart for Bazel](quickstart-bazel.md) instead.
7*28f6c2f2SEnji Cooper
8*28f6c2f2SEnji Cooper## Prerequisites
9*28f6c2f2SEnji Cooper
10*28f6c2f2SEnji CooperTo complete this tutorial, you'll need:
11*28f6c2f2SEnji Cooper
12*28f6c2f2SEnji Cooper*   A compatible operating system (e.g. Linux, macOS, Windows).
13*28f6c2f2SEnji Cooper*   A compatible C++ compiler that supports at least C++14.
14*28f6c2f2SEnji Cooper*   [CMake](https://cmake.org/) and a compatible build tool for building the
15*28f6c2f2SEnji Cooper    project.
16*28f6c2f2SEnji Cooper    *   Compatible build tools include
17*28f6c2f2SEnji Cooper        [Make](https://www.gnu.org/software/make/),
18*28f6c2f2SEnji Cooper        [Ninja](https://ninja-build.org/), and others - see
19*28f6c2f2SEnji Cooper        [CMake Generators](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html)
20*28f6c2f2SEnji Cooper        for more information.
21*28f6c2f2SEnji Cooper
22*28f6c2f2SEnji CooperSee [Supported Platforms](platforms.md) for more information about platforms
23*28f6c2f2SEnji Coopercompatible with GoogleTest.
24*28f6c2f2SEnji Cooper
25*28f6c2f2SEnji CooperIf you don't already have CMake installed, see the
26*28f6c2f2SEnji Cooper[CMake installation guide](https://cmake.org/install).
27*28f6c2f2SEnji Cooper
28*28f6c2f2SEnji Cooper{: .callout .note}
29*28f6c2f2SEnji CooperNote: The terminal commands in this tutorial show a Unix shell prompt, but the
30*28f6c2f2SEnji Coopercommands work on the Windows command line as well.
31*28f6c2f2SEnji Cooper
32*28f6c2f2SEnji Cooper## Set up a project
33*28f6c2f2SEnji Cooper
34*28f6c2f2SEnji CooperCMake uses a file named `CMakeLists.txt` to configure the build system for a
35*28f6c2f2SEnji Cooperproject. You'll use this file to set up your project and declare a dependency on
36*28f6c2f2SEnji CooperGoogleTest.
37*28f6c2f2SEnji Cooper
38*28f6c2f2SEnji CooperFirst, create a directory for your project:
39*28f6c2f2SEnji Cooper
40*28f6c2f2SEnji Cooper```
41*28f6c2f2SEnji Cooper$ mkdir my_project && cd my_project
42*28f6c2f2SEnji Cooper```
43*28f6c2f2SEnji Cooper
44*28f6c2f2SEnji CooperNext, you'll create the `CMakeLists.txt` file and declare a dependency on
45*28f6c2f2SEnji CooperGoogleTest. There are many ways to express dependencies in the CMake ecosystem;
46*28f6c2f2SEnji Cooperin this quickstart, you'll use the
47*28f6c2f2SEnji Cooper[`FetchContent` CMake module](https://cmake.org/cmake/help/latest/module/FetchContent.html).
48*28f6c2f2SEnji CooperTo do this, in your project directory (`my_project`), create a file named
49*28f6c2f2SEnji Cooper`CMakeLists.txt` with the following contents:
50*28f6c2f2SEnji Cooper
51*28f6c2f2SEnji Cooper```cmake
52*28f6c2f2SEnji Coopercmake_minimum_required(VERSION 3.14)
53*28f6c2f2SEnji Cooperproject(my_project)
54*28f6c2f2SEnji Cooper
55*28f6c2f2SEnji Cooper# GoogleTest requires at least C++14
56*28f6c2f2SEnji Cooperset(CMAKE_CXX_STANDARD 14)
57*28f6c2f2SEnji Cooperset(CMAKE_CXX_STANDARD_REQUIRED ON)
58*28f6c2f2SEnji Cooper
59*28f6c2f2SEnji Cooperinclude(FetchContent)
60*28f6c2f2SEnji CooperFetchContent_Declare(
61*28f6c2f2SEnji Cooper  googletest
62*28f6c2f2SEnji Cooper  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
63*28f6c2f2SEnji Cooper)
64*28f6c2f2SEnji Cooper# For Windows: Prevent overriding the parent project's compiler/linker settings
65*28f6c2f2SEnji Cooperset(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
66*28f6c2f2SEnji CooperFetchContent_MakeAvailable(googletest)
67*28f6c2f2SEnji Cooper```
68*28f6c2f2SEnji Cooper
69*28f6c2f2SEnji CooperThe above configuration declares a dependency on GoogleTest which is downloaded
70*28f6c2f2SEnji Cooperfrom GitHub. In the above example, `03597a01ee50ed33e9dfd640b249b4be3799d395` is
71*28f6c2f2SEnji Cooperthe Git commit hash of the GoogleTest version to use; we recommend updating the
72*28f6c2f2SEnji Cooperhash often to point to the latest version.
73*28f6c2f2SEnji Cooper
74*28f6c2f2SEnji CooperFor more information about how to create `CMakeLists.txt` files, see the
75*28f6c2f2SEnji Cooper[CMake Tutorial](https://cmake.org/cmake/help/latest/guide/tutorial/index.html).
76*28f6c2f2SEnji Cooper
77*28f6c2f2SEnji Cooper## Create and run a binary
78*28f6c2f2SEnji Cooper
79*28f6c2f2SEnji CooperWith GoogleTest declared as a dependency, you can use GoogleTest code within
80*28f6c2f2SEnji Cooperyour own project.
81*28f6c2f2SEnji Cooper
82*28f6c2f2SEnji CooperAs an example, create a file named `hello_test.cc` in your `my_project`
83*28f6c2f2SEnji Cooperdirectory with the following contents:
84*28f6c2f2SEnji Cooper
85*28f6c2f2SEnji Cooper```cpp
86*28f6c2f2SEnji Cooper#include <gtest/gtest.h>
87*28f6c2f2SEnji Cooper
88*28f6c2f2SEnji Cooper// Demonstrate some basic assertions.
89*28f6c2f2SEnji CooperTEST(HelloTest, BasicAssertions) {
90*28f6c2f2SEnji Cooper  // Expect two strings not to be equal.
91*28f6c2f2SEnji Cooper  EXPECT_STRNE("hello", "world");
92*28f6c2f2SEnji Cooper  // Expect equality.
93*28f6c2f2SEnji Cooper  EXPECT_EQ(7 * 6, 42);
94*28f6c2f2SEnji Cooper}
95*28f6c2f2SEnji Cooper```
96*28f6c2f2SEnji Cooper
97*28f6c2f2SEnji CooperGoogleTest provides [assertions](primer.md#assertions) that you use to test the
98*28f6c2f2SEnji Cooperbehavior of your code. The above sample includes the main GoogleTest header file
99*28f6c2f2SEnji Cooperand demonstrates some basic assertions.
100*28f6c2f2SEnji Cooper
101*28f6c2f2SEnji CooperTo build the code, add the following to the end of your `CMakeLists.txt` file:
102*28f6c2f2SEnji Cooper
103*28f6c2f2SEnji Cooper```cmake
104*28f6c2f2SEnji Cooperenable_testing()
105*28f6c2f2SEnji Cooper
106*28f6c2f2SEnji Cooperadd_executable(
107*28f6c2f2SEnji Cooper  hello_test
108*28f6c2f2SEnji Cooper  hello_test.cc
109*28f6c2f2SEnji Cooper)
110*28f6c2f2SEnji Coopertarget_link_libraries(
111*28f6c2f2SEnji Cooper  hello_test
112*28f6c2f2SEnji Cooper  GTest::gtest_main
113*28f6c2f2SEnji Cooper)
114*28f6c2f2SEnji Cooper
115*28f6c2f2SEnji Cooperinclude(GoogleTest)
116*28f6c2f2SEnji Coopergtest_discover_tests(hello_test)
117*28f6c2f2SEnji Cooper```
118*28f6c2f2SEnji Cooper
119*28f6c2f2SEnji CooperThe above configuration enables testing in CMake, declares the C++ test binary
120*28f6c2f2SEnji Cooperyou want to build (`hello_test`), and links it to GoogleTest (`gtest_main`). The
121*28f6c2f2SEnji Cooperlast two lines enable CMake's test runner to discover the tests included in the
122*28f6c2f2SEnji Cooperbinary, using the
123*28f6c2f2SEnji Cooper[`GoogleTest` CMake module](https://cmake.org/cmake/help/git-stage/module/GoogleTest.html).
124*28f6c2f2SEnji Cooper
125*28f6c2f2SEnji CooperNow you can build and run your test:
126*28f6c2f2SEnji Cooper
127*28f6c2f2SEnji Cooper<pre>
128*28f6c2f2SEnji Cooper<strong>my_project$ cmake -S . -B build</strong>
129*28f6c2f2SEnji Cooper-- The C compiler identification is GNU 10.2.1
130*28f6c2f2SEnji Cooper-- The CXX compiler identification is GNU 10.2.1
131*28f6c2f2SEnji Cooper...
132*28f6c2f2SEnji Cooper-- Build files have been written to: .../my_project/build
133*28f6c2f2SEnji Cooper
134*28f6c2f2SEnji Cooper<strong>my_project$ cmake --build build</strong>
135*28f6c2f2SEnji CooperScanning dependencies of target gtest
136*28f6c2f2SEnji Cooper...
137*28f6c2f2SEnji Cooper[100%] Built target gmock_main
138*28f6c2f2SEnji Cooper
139*28f6c2f2SEnji Cooper<strong>my_project$ cd build && ctest</strong>
140*28f6c2f2SEnji CooperTest project .../my_project/build
141*28f6c2f2SEnji Cooper    Start 1: HelloTest.BasicAssertions
142*28f6c2f2SEnji Cooper1/1 Test #1: HelloTest.BasicAssertions ........   Passed    0.00 sec
143*28f6c2f2SEnji Cooper
144*28f6c2f2SEnji Cooper100% tests passed, 0 tests failed out of 1
145*28f6c2f2SEnji Cooper
146*28f6c2f2SEnji CooperTotal Test time (real) =   0.01 sec
147*28f6c2f2SEnji Cooper</pre>
148*28f6c2f2SEnji Cooper
149*28f6c2f2SEnji CooperCongratulations! You've successfully built and run a test binary using
150*28f6c2f2SEnji CooperGoogleTest.
151*28f6c2f2SEnji Cooper
152*28f6c2f2SEnji Cooper## Next steps
153*28f6c2f2SEnji Cooper
154*28f6c2f2SEnji Cooper*   [Check out the Primer](primer.md) to start learning how to write simple
155*28f6c2f2SEnji Cooper    tests.
156*28f6c2f2SEnji Cooper*   [See the code samples](samples.md) for more examples showing how to use a
157*28f6c2f2SEnji Cooper    variety of GoogleTest features.
158