You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.9 KiB

  1. # Quickstart: Building with CMake
  2. This tutorial aims to get you up and running with GoogleTest using CMake. If
  3. you're using GoogleTest for the first time or need a refresher, we recommend
  4. this tutorial as a starting point. If your project uses Bazel, see the
  5. [Quickstart for Bazel](quickstart-bazel.md) instead.
  6. ## Prerequisites
  7. To complete this tutorial, you'll need:
  8. * A compatible operating system (e.g. Linux, macOS, Windows).
  9. * A compatible C++ compiler that supports at least C++11.
  10. * [CMake](https://cmake.org/) and a compatible build tool for building the
  11. project.
  12. * Compatible build tools include
  13. [Make](https://www.gnu.org/software/make/),
  14. [Ninja](https://ninja-build.org/), and others - see
  15. [CMake Generators](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html)
  16. for more information.
  17. See [Supported Platforms](platforms.md) for more information about platforms
  18. compatible with GoogleTest.
  19. If you don't already have CMake installed, see the
  20. [CMake installation guide](https://cmake.org/install).
  21. {: .callout .note}
  22. Note: The terminal commands in this tutorial show a Unix shell prompt, but the
  23. commands work on the Windows command line as well.
  24. ## Set up a project
  25. CMake uses a file named `CMakeLists.txt` to configure the build system for a
  26. project. You'll use this file to set up your project and declare a dependency on
  27. GoogleTest.
  28. First, create a directory for your project:
  29. ```
  30. $ mkdir my_project && cd my_project
  31. ```
  32. Next, you'll create the `CMakeLists.txt` file and declare a dependency on
  33. GoogleTest. There are many ways to express dependencies in the CMake ecosystem;
  34. in this quickstart, you'll use the
  35. [`FetchContent` CMake module](https://cmake.org/cmake/help/latest/module/FetchContent.html).
  36. To do this, in your project directory (`my_project`), create a file named
  37. `CMakeLists.txt` with the following contents:
  38. ```cmake
  39. cmake_minimum_required(VERSION 3.14)
  40. project(my_project)
  41. # GoogleTest requires at least C++11
  42. set(CMAKE_CXX_STANDARD 11)
  43. include(FetchContent)
  44. FetchContent_Declare(
  45. googletest
  46. URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
  47. )
  48. # For Windows: Prevent overriding the parent project's compiler/linker settings
  49. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  50. FetchContent_MakeAvailable(googletest)
  51. ```
  52. The above configuration declares a dependency on GoogleTest which is downloaded
  53. from GitHub. In the above example, `609281088cfefc76f9d0ce82e1ff6c30cc3591e5` is
  54. the Git commit hash of the GoogleTest version to use; we recommend updating the
  55. hash often to point to the latest version.
  56. For more information about how to create `CMakeLists.txt` files, see the
  57. [CMake Tutorial](https://cmake.org/cmake/help/latest/guide/tutorial/index.html).
  58. ## Create and run a binary
  59. With GoogleTest declared as a dependency, you can use GoogleTest code within
  60. your own project.
  61. As an example, create a file named `hello_test.cc` in your `my_project`
  62. directory with the following contents:
  63. ```cpp
  64. #include <gtest/gtest.h>
  65. // Demonstrate some basic assertions.
  66. TEST(HelloTest, BasicAssertions) {
  67. // Expect two strings not to be equal.
  68. EXPECT_STRNE("hello", "world");
  69. // Expect equality.
  70. EXPECT_EQ(7 * 6, 42);
  71. }
  72. ```
  73. GoogleTest provides [assertions](primer.md#assertions) that you use to test the
  74. behavior of your code. The above sample includes the main GoogleTest header file
  75. and demonstrates some basic assertions.
  76. To build the code, add the following to the end of your `CMakeLists.txt` file:
  77. ```cmake
  78. enable_testing()
  79. add_executable(
  80. hello_test
  81. hello_test.cc
  82. )
  83. target_link_libraries(
  84. hello_test
  85. gtest_main
  86. )
  87. include(GoogleTest)
  88. gtest_discover_tests(hello_test)
  89. ```
  90. The above configuration enables testing in CMake, declares the C++ test binary
  91. you want to build (`hello_test`), and links it to GoogleTest (`gtest_main`). The
  92. last two lines enable CMake's test runner to discover the tests included in the
  93. binary, using the
  94. [`GoogleTest` CMake module](https://cmake.org/cmake/help/git-stage/module/GoogleTest.html).
  95. Now you can build and run your test:
  96. <pre>
  97. <strong>my_project$ cmake -S . -B build</strong>
  98. -- The C compiler identification is GNU 10.2.1
  99. -- The CXX compiler identification is GNU 10.2.1
  100. ...
  101. -- Build files have been written to: .../my_project/build
  102. <strong>my_project$ cmake --build build</strong>
  103. Scanning dependencies of target gtest
  104. ...
  105. [100%] Built target gmock_main
  106. <strong>my_project$ cd build && ctest</strong>
  107. Test project .../my_project/build
  108. Start 1: HelloTest.BasicAssertions
  109. 1/1 Test #1: HelloTest.BasicAssertions ........ Passed 0.00 sec
  110. 100% tests passed, 0 tests failed out of 1
  111. Total Test time (real) = 0.01 sec
  112. </pre>
  113. Congratulations! You've successfully built and run a test binary using
  114. GoogleTest.
  115. ## Next steps
  116. * [Check out the Primer](primer.md) to start learning how to write simple
  117. tests.
  118. * [See the code samples](samples.md) for more examples showing how to use a
  119. variety of GoogleTest features.