The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

215 lines
8.5 KiB

2 months ago
  1. ### Generic Build Instructions
  2. #### Setup
  3. To build GoogleTest and your tests that use it, you need to tell your build
  4. system where to find its headers and source files. The exact way to do it
  5. depends on which build system you use, and is usually straightforward.
  6. ### Build with CMake
  7. GoogleTest comes with a CMake build script
  8. ([CMakeLists.txt](https://github.com/google/googletest/blob/master/CMakeLists.txt))
  9. that can be used on a wide range of platforms ("C" stands for cross-platform.).
  10. If you don't have CMake installed already, you can download it for free from
  11. <http://www.cmake.org/>.
  12. CMake works by generating native makefiles or build projects that can be used in
  13. the compiler environment of your choice. You can either build GoogleTest as a
  14. standalone project or it can be incorporated into an existing CMake build for
  15. another project.
  16. #### Standalone CMake Project
  17. When building GoogleTest as a standalone project, the typical workflow starts
  18. with
  19. ```
  20. git clone https://github.com/google/googletest.git -b release-1.10.0
  21. cd googletest # Main directory of the cloned repository.
  22. mkdir build # Create a directory to hold the build output.
  23. cd build
  24. cmake .. # Generate native build scripts for GoogleTest.
  25. ```
  26. The above command also includes GoogleMock by default. And so, if you want to
  27. build only GoogleTest, you should replace the last command with
  28. ```
  29. cmake .. -DBUILD_GMOCK=OFF
  30. ```
  31. If you are on a \*nix system, you should now see a Makefile in the current
  32. directory. Just type `make` to build GoogleTest. And then you can simply install
  33. GoogleTest if you are a system administrator.
  34. ```
  35. make
  36. sudo make install # Install in /usr/local/ by default
  37. ```
  38. If you use Windows and have Visual Studio installed, a `gtest.sln` file and
  39. several `.vcproj` files will be created. You can then build them using Visual
  40. Studio.
  41. On Mac OS X with Xcode installed, a `.xcodeproj` file will be generated.
  42. #### Incorporating Into An Existing CMake Project
  43. If you want to use GoogleTest in a project which already uses CMake, the easiest
  44. way is to get installed libraries and headers.
  45. * Import GoogleTest by using `find_package` (or `pkg_check_modules`). For
  46. example, if `find_package(GTest CONFIG REQUIRED)` succeeds, you can use the
  47. libraries as `GTest::gtest`, `GTest::gmock`.
  48. And a more robust and flexible approach is to build GoogleTest as part of that
  49. project directly. This is done by making the GoogleTest source code available to
  50. the main build and adding it using CMake's `add_subdirectory()` command. This
  51. has the significant advantage that the same compiler and linker settings are
  52. used between GoogleTest and the rest of your project, so issues associated with
  53. using incompatible libraries (eg debug/release), etc. are avoided. This is
  54. particularly useful on Windows. Making GoogleTest's source code available to the
  55. main build can be done a few different ways:
  56. * Download the GoogleTest source code manually and place it at a known
  57. location. This is the least flexible approach and can make it more difficult
  58. to use with continuous integration systems, etc.
  59. * Embed the GoogleTest source code as a direct copy in the main project's
  60. source tree. This is often the simplest approach, but is also the hardest to
  61. keep up to date. Some organizations may not permit this method.
  62. * Add GoogleTest as a git submodule or equivalent. This may not always be
  63. possible or appropriate. Git submodules, for example, have their own set of
  64. advantages and drawbacks.
  65. * Use CMake to download GoogleTest as part of the build's configure step. This
  66. approach doesn't have the limitations of the other methods.
  67. The last of the above methods is implemented with a small piece of CMake code
  68. that downloads and pulls the GoogleTest code into the main build.
  69. Just add to your `CMakeLists.txt`:
  70. ```cmake
  71. include(FetchContent)
  72. FetchContent_Declare(
  73. googletest
  74. # Specify the commit you depend on and update it regularly.
  75. URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
  76. )
  77. # For Windows: Prevent overriding the parent project's compiler/linker settings
  78. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  79. FetchContent_MakeAvailable(googletest)
  80. # Now simply link against gtest or gtest_main as needed. Eg
  81. add_executable(example example.cpp)
  82. target_link_libraries(example gtest_main)
  83. add_test(NAME example_test COMMAND example)
  84. ```
  85. Note that this approach requires CMake 3.14 or later due to its use of the
  86. `FetchContent_MakeAvailable()` command.
  87. ##### Visual Studio Dynamic vs Static Runtimes
  88. By default, new Visual Studio projects link the C runtimes dynamically but
  89. GoogleTest links them statically. This will generate an error that looks
  90. something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch
  91. detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value
  92. 'MDd_DynamicDebug' in main.obj
  93. GoogleTest already has a CMake option for this: `gtest_force_shared_crt`
  94. Enabling this option will make gtest link the runtimes dynamically too, and
  95. match the project in which it is included.
  96. #### C++ Standard Version
  97. An environment that supports C++11 is required in order to successfully build
  98. GoogleTest. One way to ensure this is to specify the standard in the top-level
  99. project, for example by using the `set(CMAKE_CXX_STANDARD 11)` command. If this
  100. is not feasible, for example in a C project using GoogleTest for validation,
  101. then it can be specified by adding it to the options for cmake via the
  102. `DCMAKE_CXX_FLAGS` option.
  103. ### Tweaking GoogleTest
  104. GoogleTest can be used in diverse environments. The default configuration may
  105. not work (or may not work well) out of the box in some environments. However,
  106. you can easily tweak GoogleTest by defining control macros on the compiler
  107. command line. Generally, these macros are named like `GTEST_XYZ` and you define
  108. them to either 1 or 0 to enable or disable a certain feature.
  109. We list the most frequently used macros below. For a complete list, see file
  110. [include/gtest/internal/gtest-port.h](https://github.com/google/googletest/blob/master/googletest/include/gtest/internal/gtest-port.h).
  111. ### Multi-threaded Tests
  112. GoogleTest is thread-safe where the pthread library is available. After
  113. `#include "gtest/gtest.h"`, you can check the
  114. `GTEST_IS_THREADSAFE` macro to see whether this is the case (yes if the macro is
  115. `#defined` to 1, no if it's undefined.).
  116. If GoogleTest doesn't correctly detect whether pthread is available in your
  117. environment, you can force it with
  118. -DGTEST_HAS_PTHREAD=1
  119. or
  120. -DGTEST_HAS_PTHREAD=0
  121. When GoogleTest uses pthread, you may need to add flags to your compiler and/or
  122. linker to select the pthread library, or you'll get link errors. If you use the
  123. CMake script, this is taken care of for you. If you use your own build script,
  124. you'll need to read your compiler and linker's manual to figure out what flags
  125. to add.
  126. ### As a Shared Library (DLL)
  127. GoogleTest is compact, so most users can build and link it as a static library
  128. for the simplicity. You can choose to use GoogleTest as a shared library (known
  129. as a DLL on Windows) if you prefer.
  130. To compile *gtest* as a shared library, add
  131. -DGTEST_CREATE_SHARED_LIBRARY=1
  132. to the compiler flags. You'll also need to tell the linker to produce a shared
  133. library instead - consult your linker's manual for how to do it.
  134. To compile your *tests* that use the gtest shared library, add
  135. -DGTEST_LINKED_AS_SHARED_LIBRARY=1
  136. to the compiler flags.
  137. Note: while the above steps aren't technically necessary today when using some
  138. compilers (e.g. GCC), they may become necessary in the future, if we decide to
  139. improve the speed of loading the library (see
  140. <http://gcc.gnu.org/wiki/Visibility> for details). Therefore you are recommended
  141. to always add the above flags when using GoogleTest as a shared library.
  142. Otherwise a future release of GoogleTest may break your build script.
  143. ### Avoiding Macro Name Clashes
  144. In C++, macros don't obey namespaces. Therefore two libraries that both define a
  145. macro of the same name will clash if you `#include` both definitions. In case a
  146. GoogleTest macro clashes with another library, you can force GoogleTest to
  147. rename its macro to avoid the conflict.
  148. Specifically, if both GoogleTest and some other code define macro FOO, you can
  149. add
  150. -DGTEST_DONT_DEFINE_FOO=1
  151. to the compiler flags to tell GoogleTest to change the macro's name from `FOO`
  152. to `GTEST_FOO`. Currently `FOO` can be `FAIL`, `SUCCEED`, or `TEST`. For
  153. example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write
  154. GTEST_TEST(SomeTest, DoesThis) { ... }
  155. instead of
  156. TEST(SomeTest, DoesThis) { ... }
  157. in order to define a test.