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.

244 lines
9.7 KiB

  1. ### Generic Build Instructions
  2. #### Setup
  3. To build Google Test 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. Google Test 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 Google Test 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 Google Test as a standalone project, the typical workflow starts
  18. with:
  19. mkdir mybuild # Create a directory to hold the build output.
  20. cd mybuild
  21. cmake ${GTEST_DIR} # Generate native build scripts.
  22. If you want to build Google Test's samples, you should replace the last command
  23. with
  24. cmake -Dgtest_build_samples=ON ${GTEST_DIR}
  25. If you are on a \*nix system, you should now see a Makefile in the current
  26. directory. Just type 'make' to build gtest.
  27. If you use Windows and have Visual Studio installed, a `gtest.sln` file and
  28. several `.vcproj` files will be created. You can then build them using Visual
  29. Studio.
  30. On Mac OS X with Xcode installed, a `.xcodeproj` file will be generated.
  31. #### Incorporating Into An Existing CMake Project
  32. If you want to use gtest in a project which already uses CMake, then a more
  33. robust and flexible approach is to build gtest as part of that project directly.
  34. This is done by making the GoogleTest source code available to the main build
  35. and adding it using CMake's `add_subdirectory()` command. This has the
  36. significant advantage that the same compiler and linker settings are used
  37. between gtest and the rest of your project, so issues associated with using
  38. incompatible libraries (eg debug/release), etc. are avoided. This is
  39. particularly useful on Windows. Making GoogleTest's source code available to the
  40. main build can be done a few different ways:
  41. * Download the GoogleTest source code manually and place it at a known
  42. location. This is the least flexible approach and can make it more difficult
  43. to use with continuous integration systems, etc.
  44. * Embed the GoogleTest source code as a direct copy in the main project's
  45. source tree. This is often the simplest approach, but is also the hardest to
  46. keep up to date. Some organizations may not permit this method.
  47. * Add GoogleTest as a git submodule or equivalent. This may not always be
  48. possible or appropriate. Git submodules, for example, have their own set of
  49. advantages and drawbacks.
  50. * Use CMake to download GoogleTest as part of the build's configure step. This
  51. is just a little more complex, but doesn't have the limitations of the other
  52. methods.
  53. The last of the above methods is implemented with a small piece of CMake code in
  54. a separate file (e.g. `CMakeLists.txt.in`) which is copied to the build area and
  55. then invoked as a sub-build _during the CMake stage_. That directory is then
  56. pulled into the main build with `add_subdirectory()`. For example:
  57. New file `CMakeLists.txt.in`:
  58. ```cmake
  59. cmake_minimum_required(VERSION 2.8.2)
  60. project(googletest-download NONE)
  61. include(ExternalProject)
  62. ExternalProject_Add(googletest
  63. GIT_REPOSITORY https://github.com/google/googletest.git
  64. GIT_TAG master
  65. SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
  66. BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
  67. CONFIGURE_COMMAND ""
  68. BUILD_COMMAND ""
  69. INSTALL_COMMAND ""
  70. TEST_COMMAND ""
  71. )
  72. ```
  73. Existing build's `CMakeLists.txt`:
  74. ```cmake
  75. # Download and unpack googletest at configure time
  76. configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
  77. execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  78. RESULT_VARIABLE result
  79. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
  80. if(result)
  81. message(FATAL_ERROR "CMake step for googletest failed: ${result}")
  82. endif()
  83. execute_process(COMMAND ${CMAKE_COMMAND} --build .
  84. RESULT_VARIABLE result
  85. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
  86. if(result)
  87. message(FATAL_ERROR "Build step for googletest failed: ${result}")
  88. endif()
  89. # Prevent overriding the parent project's compiler/linker
  90. # settings on Windows
  91. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  92. # Add googletest directly to our build. This defines
  93. # the gtest and gtest_main targets.
  94. add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
  95. ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
  96. EXCLUDE_FROM_ALL)
  97. # The gtest/gtest_main targets carry header search path
  98. # dependencies automatically when using CMake 2.8.11 or
  99. # later. Otherwise we have to add them here ourselves.
  100. if (CMAKE_VERSION VERSION_LESS 2.8.11)
  101. include_directories("${gtest_SOURCE_DIR}/include")
  102. endif()
  103. # Now simply link against gtest or gtest_main as needed. Eg
  104. add_executable(example example.cpp)
  105. target_link_libraries(example gtest_main)
  106. add_test(NAME example_test COMMAND example)
  107. ```
  108. Note that this approach requires CMake 2.8.2 or later due to its use of the
  109. `ExternalProject_Add()` command. The above technique is discussed in more detail
  110. in [this separate article](http://crascit.com/2015/07/25/cmake-gtest/) which
  111. also contains a link to a fully generalized implementation of the technique.
  112. ##### Visual Studio Dynamic vs Static Runtimes
  113. By default, new Visual Studio projects link the C runtimes dynamically but
  114. Google Test links them statically. This will generate an error that looks
  115. something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch
  116. detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value
  117. 'MDd_DynamicDebug' in main.obj
  118. Google Test already has a CMake option for this: `gtest_force_shared_crt`
  119. Enabling this option will make gtest link the runtimes dynamically too, and
  120. match the project in which it is included.
  121. #### C++ Standard Version
  122. An environment that supports C++11 is required in order to successfully build
  123. Google Test. One way to ensure this is to specify the standard in the top-level
  124. project, for example by using the `set(CMAKE_CXX_STANDARD 11)` command. If this
  125. is not feasible, for example in a C project using Google Test for validation,
  126. then it can be specified by adding it to the options for cmake via the
  127. `DCMAKE_CXX_FLAGS` option.
  128. ### Tweaking Google Test
  129. Google Test can be used in diverse environments. The default configuration may
  130. not work (or may not work well) out of the box in some environments. However,
  131. you can easily tweak Google Test by defining control macros on the compiler
  132. command line. Generally, these macros are named like `GTEST_XYZ` and you define
  133. them to either 1 or 0 to enable or disable a certain feature.
  134. We list the most frequently used macros below. For a complete list, see file
  135. [include/gtest/internal/gtest-port.h](https://github.com/google/googletest/blob/master/googletest/include/gtest/internal/gtest-port.h).
  136. ### Multi-threaded Tests
  137. Google Test is thread-safe where the pthread library is available. After
  138. `#include "gtest/gtest.h"`, you can check the
  139. `GTEST_IS_THREADSAFE` macro to see whether this is the case (yes if the macro is
  140. `#defined` to 1, no if it's undefined.).
  141. If Google Test doesn't correctly detect whether pthread is available in your
  142. environment, you can force it with
  143. -DGTEST_HAS_PTHREAD=1
  144. or
  145. -DGTEST_HAS_PTHREAD=0
  146. When Google Test uses pthread, you may need to add flags to your compiler and/or
  147. linker to select the pthread library, or you'll get link errors. If you use the
  148. CMake script or the deprecated Autotools script, this is taken care of for you.
  149. If you use your own build script, you'll need to read your compiler and linker's
  150. manual to figure out what flags to add.
  151. ### As a Shared Library (DLL)
  152. Google Test is compact, so most users can build and link it as a static library
  153. for the simplicity. You can choose to use Google Test as a shared library (known
  154. as a DLL on Windows) if you prefer.
  155. To compile *gtest* as a shared library, add
  156. -DGTEST_CREATE_SHARED_LIBRARY=1
  157. to the compiler flags. You'll also need to tell the linker to produce a shared
  158. library instead - consult your linker's manual for how to do it.
  159. To compile your *tests* that use the gtest shared library, add
  160. -DGTEST_LINKED_AS_SHARED_LIBRARY=1
  161. to the compiler flags.
  162. Note: while the above steps aren't technically necessary today when using some
  163. compilers (e.g. GCC), they may become necessary in the future, if we decide to
  164. improve the speed of loading the library (see
  165. <http://gcc.gnu.org/wiki/Visibility> for details). Therefore you are recommended
  166. to always add the above flags when using Google Test as a shared library.
  167. Otherwise a future release of Google Test may break your build script.
  168. ### Avoiding Macro Name Clashes
  169. In C++, macros don't obey namespaces. Therefore two libraries that both define a
  170. macro of the same name will clash if you `#include` both definitions. In case a
  171. Google Test macro clashes with another library, you can force Google Test to
  172. rename its macro to avoid the conflict.
  173. Specifically, if both Google Test and some other code define macro FOO, you can
  174. add
  175. -DGTEST_DONT_DEFINE_FOO=1
  176. to the compiler flags to tell Google Test to change the macro's name from `FOO`
  177. to `GTEST_FOO`. Currently `FOO` can be `FAIL`, `SUCCEED`, or `TEST`. For
  178. example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write
  179. GTEST_TEST(SomeTest, DoesThis) { ... }
  180. instead of
  181. TEST(SomeTest, DoesThis) { ... }
  182. in order to define a test.