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