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