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