1*fb1b10abSAndroid Build Coastguard Worker### Generic Build Instructions 2*fb1b10abSAndroid Build Coastguard Worker 3*fb1b10abSAndroid Build Coastguard Worker#### Setup 4*fb1b10abSAndroid Build Coastguard Worker 5*fb1b10abSAndroid Build Coastguard WorkerTo build GoogleTest and your tests that use it, you need to tell your build 6*fb1b10abSAndroid Build Coastguard Workersystem where to find its headers and source files. The exact way to do it 7*fb1b10abSAndroid Build Coastguard Workerdepends on which build system you use, and is usually straightforward. 8*fb1b10abSAndroid Build Coastguard Worker 9*fb1b10abSAndroid Build Coastguard Worker### Build with CMake 10*fb1b10abSAndroid Build Coastguard Worker 11*fb1b10abSAndroid Build Coastguard WorkerGoogleTest comes with a CMake build script 12*fb1b10abSAndroid Build Coastguard Worker([CMakeLists.txt](https://github.com/google/googletest/blob/master/CMakeLists.txt)) 13*fb1b10abSAndroid Build Coastguard Workerthat can be used on a wide range of platforms ("C" stands for cross-platform.). 14*fb1b10abSAndroid Build Coastguard WorkerIf you don't have CMake installed already, you can download it for free from 15*fb1b10abSAndroid Build Coastguard Worker<http://www.cmake.org/>. 16*fb1b10abSAndroid Build Coastguard Worker 17*fb1b10abSAndroid Build Coastguard WorkerCMake works by generating native makefiles or build projects that can be used in 18*fb1b10abSAndroid Build Coastguard Workerthe compiler environment of your choice. You can either build GoogleTest as a 19*fb1b10abSAndroid Build Coastguard Workerstandalone project or it can be incorporated into an existing CMake build for 20*fb1b10abSAndroid Build Coastguard Workeranother project. 21*fb1b10abSAndroid Build Coastguard Worker 22*fb1b10abSAndroid Build Coastguard Worker#### Standalone CMake Project 23*fb1b10abSAndroid Build Coastguard Worker 24*fb1b10abSAndroid Build Coastguard WorkerWhen building GoogleTest as a standalone project, the typical workflow starts 25*fb1b10abSAndroid Build Coastguard Workerwith 26*fb1b10abSAndroid Build Coastguard Worker 27*fb1b10abSAndroid Build Coastguard Worker``` 28*fb1b10abSAndroid Build Coastguard Workergit clone https://github.com/google/googletest.git -b release-1.11.0 29*fb1b10abSAndroid Build Coastguard Workercd googletest # Main directory of the cloned repository. 30*fb1b10abSAndroid Build Coastguard Workermkdir build # Create a directory to hold the build output. 31*fb1b10abSAndroid Build Coastguard Workercd build 32*fb1b10abSAndroid Build Coastguard Workercmake .. # Generate native build scripts for GoogleTest. 33*fb1b10abSAndroid Build Coastguard Worker``` 34*fb1b10abSAndroid Build Coastguard Worker 35*fb1b10abSAndroid Build Coastguard WorkerThe above command also includes GoogleMock by default. And so, if you want to 36*fb1b10abSAndroid Build Coastguard Workerbuild only GoogleTest, you should replace the last command with 37*fb1b10abSAndroid Build Coastguard Worker 38*fb1b10abSAndroid Build Coastguard Worker``` 39*fb1b10abSAndroid Build Coastguard Workercmake .. -DBUILD_GMOCK=OFF 40*fb1b10abSAndroid Build Coastguard Worker``` 41*fb1b10abSAndroid Build Coastguard Worker 42*fb1b10abSAndroid Build Coastguard WorkerIf you are on a \*nix system, you should now see a Makefile in the current 43*fb1b10abSAndroid Build Coastguard Workerdirectory. Just type `make` to build GoogleTest. And then you can simply install 44*fb1b10abSAndroid Build Coastguard WorkerGoogleTest if you are a system administrator. 45*fb1b10abSAndroid Build Coastguard Worker 46*fb1b10abSAndroid Build Coastguard Worker``` 47*fb1b10abSAndroid Build Coastguard Workermake 48*fb1b10abSAndroid Build Coastguard Workersudo make install # Install in /usr/local/ by default 49*fb1b10abSAndroid Build Coastguard Worker``` 50*fb1b10abSAndroid Build Coastguard Worker 51*fb1b10abSAndroid Build Coastguard WorkerIf you use Windows and have Visual Studio installed, a `gtest.sln` file and 52*fb1b10abSAndroid Build Coastguard Workerseveral `.vcproj` files will be created. You can then build them using Visual 53*fb1b10abSAndroid Build Coastguard WorkerStudio. 54*fb1b10abSAndroid Build Coastguard Worker 55*fb1b10abSAndroid Build Coastguard WorkerOn Mac OS X with Xcode installed, a `.xcodeproj` file will be generated. 56*fb1b10abSAndroid Build Coastguard Worker 57*fb1b10abSAndroid Build Coastguard Worker#### Incorporating Into An Existing CMake Project 58*fb1b10abSAndroid Build Coastguard Worker 59*fb1b10abSAndroid Build Coastguard WorkerIf you want to use GoogleTest in a project which already uses CMake, the easiest 60*fb1b10abSAndroid Build Coastguard Workerway is to get installed libraries and headers. 61*fb1b10abSAndroid Build Coastguard Worker 62*fb1b10abSAndroid Build Coastguard Worker* Import GoogleTest by using `find_package` (or `pkg_check_modules`). For 63*fb1b10abSAndroid Build Coastguard Worker example, if `find_package(GTest CONFIG REQUIRED)` succeeds, you can use the 64*fb1b10abSAndroid Build Coastguard Worker libraries as `GTest::gtest`, `GTest::gmock`. 65*fb1b10abSAndroid Build Coastguard Worker 66*fb1b10abSAndroid Build Coastguard WorkerAnd a more robust and flexible approach is to build GoogleTest as part of that 67*fb1b10abSAndroid Build Coastguard Workerproject directly. This is done by making the GoogleTest source code available to 68*fb1b10abSAndroid Build Coastguard Workerthe main build and adding it using CMake's `add_subdirectory()` command. This 69*fb1b10abSAndroid Build Coastguard Workerhas the significant advantage that the same compiler and linker settings are 70*fb1b10abSAndroid Build Coastguard Workerused between GoogleTest and the rest of your project, so issues associated with 71*fb1b10abSAndroid Build Coastguard Workerusing incompatible libraries (eg debug/release), etc. are avoided. This is 72*fb1b10abSAndroid Build Coastguard Workerparticularly useful on Windows. Making GoogleTest's source code available to the 73*fb1b10abSAndroid Build Coastguard Workermain build can be done a few different ways: 74*fb1b10abSAndroid Build Coastguard Worker 75*fb1b10abSAndroid Build Coastguard Worker* Download the GoogleTest source code manually and place it at a known 76*fb1b10abSAndroid Build Coastguard Worker location. This is the least flexible approach and can make it more difficult 77*fb1b10abSAndroid Build Coastguard Worker to use with continuous integration systems, etc. 78*fb1b10abSAndroid Build Coastguard Worker* Embed the GoogleTest source code as a direct copy in the main project's 79*fb1b10abSAndroid Build Coastguard Worker source tree. This is often the simplest approach, but is also the hardest to 80*fb1b10abSAndroid Build Coastguard Worker keep up to date. Some organizations may not permit this method. 81*fb1b10abSAndroid Build Coastguard Worker* Add GoogleTest as a git submodule or equivalent. This may not always be 82*fb1b10abSAndroid Build Coastguard Worker possible or appropriate. Git submodules, for example, have their own set of 83*fb1b10abSAndroid Build Coastguard Worker advantages and drawbacks. 84*fb1b10abSAndroid Build Coastguard Worker* Use CMake to download GoogleTest as part of the build's configure step. This 85*fb1b10abSAndroid Build Coastguard Worker approach doesn't have the limitations of the other methods. 86*fb1b10abSAndroid Build Coastguard Worker 87*fb1b10abSAndroid Build Coastguard WorkerThe last of the above methods is implemented with a small piece of CMake code 88*fb1b10abSAndroid Build Coastguard Workerthat downloads and pulls the GoogleTest code into the main build. 89*fb1b10abSAndroid Build Coastguard Worker 90*fb1b10abSAndroid Build Coastguard WorkerJust add to your `CMakeLists.txt`: 91*fb1b10abSAndroid Build Coastguard Worker 92*fb1b10abSAndroid Build Coastguard Worker```cmake 93*fb1b10abSAndroid Build Coastguard Workerinclude(FetchContent) 94*fb1b10abSAndroid Build Coastguard WorkerFetchContent_Declare( 95*fb1b10abSAndroid Build Coastguard Worker googletest 96*fb1b10abSAndroid Build Coastguard Worker # Specify the commit you depend on and update it regularly. 97*fb1b10abSAndroid Build Coastguard Worker URL https://github.com/google/googletest/archive/e2239ee6043f73722e7aa812a459f54a28552929.zip 98*fb1b10abSAndroid Build Coastguard Worker) 99*fb1b10abSAndroid Build Coastguard Worker# For Windows: Prevent overriding the parent project's compiler/linker settings 100*fb1b10abSAndroid Build Coastguard Workerset(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 101*fb1b10abSAndroid Build Coastguard WorkerFetchContent_MakeAvailable(googletest) 102*fb1b10abSAndroid Build Coastguard Worker 103*fb1b10abSAndroid Build Coastguard Worker# Now simply link against gtest or gtest_main as needed. Eg 104*fb1b10abSAndroid Build Coastguard Workeradd_executable(example example.cpp) 105*fb1b10abSAndroid Build Coastguard Workertarget_link_libraries(example gtest_main) 106*fb1b10abSAndroid Build Coastguard Workeradd_test(NAME example_test COMMAND example) 107*fb1b10abSAndroid Build Coastguard Worker``` 108*fb1b10abSAndroid Build Coastguard Worker 109*fb1b10abSAndroid Build Coastguard WorkerNote that this approach requires CMake 3.14 or later due to its use of the 110*fb1b10abSAndroid Build Coastguard Worker`FetchContent_MakeAvailable()` command. 111*fb1b10abSAndroid Build Coastguard Worker 112*fb1b10abSAndroid Build Coastguard Worker##### Visual Studio Dynamic vs Static Runtimes 113*fb1b10abSAndroid Build Coastguard Worker 114*fb1b10abSAndroid Build Coastguard WorkerBy default, new Visual Studio projects link the C runtimes dynamically but 115*fb1b10abSAndroid Build Coastguard WorkerGoogleTest links them statically. This will generate an error that looks 116*fb1b10abSAndroid Build Coastguard Workersomething like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch 117*fb1b10abSAndroid Build Coastguard Workerdetected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 118*fb1b10abSAndroid Build Coastguard Worker'MDd_DynamicDebug' in main.obj 119*fb1b10abSAndroid Build Coastguard Worker 120*fb1b10abSAndroid Build Coastguard WorkerGoogleTest already has a CMake option for this: `gtest_force_shared_crt` 121*fb1b10abSAndroid Build Coastguard Worker 122*fb1b10abSAndroid Build Coastguard WorkerEnabling this option will make gtest link the runtimes dynamically too, and 123*fb1b10abSAndroid Build Coastguard Workermatch the project in which it is included. 124*fb1b10abSAndroid Build Coastguard Worker 125*fb1b10abSAndroid Build Coastguard Worker#### C++ Standard Version 126*fb1b10abSAndroid Build Coastguard Worker 127*fb1b10abSAndroid Build Coastguard WorkerAn environment that supports C++11 is required in order to successfully build 128*fb1b10abSAndroid Build Coastguard WorkerGoogleTest. One way to ensure this is to specify the standard in the top-level 129*fb1b10abSAndroid Build Coastguard Workerproject, for example by using the `set(CMAKE_CXX_STANDARD 11)` command. If this 130*fb1b10abSAndroid Build Coastguard Workeris not feasible, for example in a C project using GoogleTest for validation, 131*fb1b10abSAndroid Build Coastguard Workerthen it can be specified by adding it to the options for cmake via the 132*fb1b10abSAndroid Build Coastguard Worker`DCMAKE_CXX_FLAGS` option. 133*fb1b10abSAndroid Build Coastguard Worker 134*fb1b10abSAndroid Build Coastguard Worker### Tweaking GoogleTest 135*fb1b10abSAndroid Build Coastguard Worker 136*fb1b10abSAndroid Build Coastguard WorkerGoogleTest can be used in diverse environments. The default configuration may 137*fb1b10abSAndroid Build Coastguard Workernot work (or may not work well) out of the box in some environments. However, 138*fb1b10abSAndroid Build Coastguard Workeryou can easily tweak GoogleTest by defining control macros on the compiler 139*fb1b10abSAndroid Build Coastguard Workercommand line. Generally, these macros are named like `GTEST_XYZ` and you define 140*fb1b10abSAndroid Build Coastguard Workerthem to either 1 or 0 to enable or disable a certain feature. 141*fb1b10abSAndroid Build Coastguard Worker 142*fb1b10abSAndroid Build Coastguard WorkerWe list the most frequently used macros below. For a complete list, see file 143*fb1b10abSAndroid Build Coastguard Worker[include/gtest/internal/gtest-port.h](https://github.com/google/googletest/blob/master/googletest/include/gtest/internal/gtest-port.h). 144*fb1b10abSAndroid Build Coastguard Worker 145*fb1b10abSAndroid Build Coastguard Worker### Multi-threaded Tests 146*fb1b10abSAndroid Build Coastguard Worker 147*fb1b10abSAndroid Build Coastguard WorkerGoogleTest is thread-safe where the pthread library is available. After 148*fb1b10abSAndroid Build Coastguard Worker`#include "gtest/gtest.h"`, you can check the 149*fb1b10abSAndroid Build Coastguard Worker`GTEST_IS_THREADSAFE` macro to see whether this is the case (yes if the macro is 150*fb1b10abSAndroid Build Coastguard Worker`#defined` to 1, no if it's undefined.). 151*fb1b10abSAndroid Build Coastguard Worker 152*fb1b10abSAndroid Build Coastguard WorkerIf GoogleTest doesn't correctly detect whether pthread is available in your 153*fb1b10abSAndroid Build Coastguard Workerenvironment, you can force it with 154*fb1b10abSAndroid Build Coastguard Worker 155*fb1b10abSAndroid Build Coastguard Worker -DGTEST_HAS_PTHREAD=1 156*fb1b10abSAndroid Build Coastguard Worker 157*fb1b10abSAndroid Build Coastguard Workeror 158*fb1b10abSAndroid Build Coastguard Worker 159*fb1b10abSAndroid Build Coastguard Worker -DGTEST_HAS_PTHREAD=0 160*fb1b10abSAndroid Build Coastguard Worker 161*fb1b10abSAndroid Build Coastguard WorkerWhen GoogleTest uses pthread, you may need to add flags to your compiler and/or 162*fb1b10abSAndroid Build Coastguard Workerlinker to select the pthread library, or you'll get link errors. If you use the 163*fb1b10abSAndroid Build Coastguard WorkerCMake script, this is taken care of for you. If you use your own build script, 164*fb1b10abSAndroid Build Coastguard Workeryou'll need to read your compiler and linker's manual to figure out what flags 165*fb1b10abSAndroid Build Coastguard Workerto add. 166*fb1b10abSAndroid Build Coastguard Worker 167*fb1b10abSAndroid Build Coastguard Worker### As a Shared Library (DLL) 168*fb1b10abSAndroid Build Coastguard Worker 169*fb1b10abSAndroid Build Coastguard WorkerGoogleTest is compact, so most users can build and link it as a static library 170*fb1b10abSAndroid Build Coastguard Workerfor the simplicity. You can choose to use GoogleTest as a shared library (known 171*fb1b10abSAndroid Build Coastguard Workeras a DLL on Windows) if you prefer. 172*fb1b10abSAndroid Build Coastguard Worker 173*fb1b10abSAndroid Build Coastguard WorkerTo compile *gtest* as a shared library, add 174*fb1b10abSAndroid Build Coastguard Worker 175*fb1b10abSAndroid Build Coastguard Worker -DGTEST_CREATE_SHARED_LIBRARY=1 176*fb1b10abSAndroid Build Coastguard Worker 177*fb1b10abSAndroid Build Coastguard Workerto the compiler flags. You'll also need to tell the linker to produce a shared 178*fb1b10abSAndroid Build Coastguard Workerlibrary instead - consult your linker's manual for how to do it. 179*fb1b10abSAndroid Build Coastguard Worker 180*fb1b10abSAndroid Build Coastguard WorkerTo compile your *tests* that use the gtest shared library, add 181*fb1b10abSAndroid Build Coastguard Worker 182*fb1b10abSAndroid Build Coastguard Worker -DGTEST_LINKED_AS_SHARED_LIBRARY=1 183*fb1b10abSAndroid Build Coastguard Worker 184*fb1b10abSAndroid Build Coastguard Workerto the compiler flags. 185*fb1b10abSAndroid Build Coastguard Worker 186*fb1b10abSAndroid Build Coastguard WorkerNote: while the above steps aren't technically necessary today when using some 187*fb1b10abSAndroid Build Coastguard Workercompilers (e.g. GCC), they may become necessary in the future, if we decide to 188*fb1b10abSAndroid Build Coastguard Workerimprove the speed of loading the library (see 189*fb1b10abSAndroid Build Coastguard Worker<http://gcc.gnu.org/wiki/Visibility> for details). Therefore you are recommended 190*fb1b10abSAndroid Build Coastguard Workerto always add the above flags when using GoogleTest as a shared library. 191*fb1b10abSAndroid Build Coastguard WorkerOtherwise a future release of GoogleTest may break your build script. 192*fb1b10abSAndroid Build Coastguard Worker 193*fb1b10abSAndroid Build Coastguard Worker### Avoiding Macro Name Clashes 194*fb1b10abSAndroid Build Coastguard Worker 195*fb1b10abSAndroid Build Coastguard WorkerIn C++, macros don't obey namespaces. Therefore two libraries that both define a 196*fb1b10abSAndroid Build Coastguard Workermacro of the same name will clash if you `#include` both definitions. In case a 197*fb1b10abSAndroid Build Coastguard WorkerGoogleTest macro clashes with another library, you can force GoogleTest to 198*fb1b10abSAndroid Build Coastguard Workerrename its macro to avoid the conflict. 199*fb1b10abSAndroid Build Coastguard Worker 200*fb1b10abSAndroid Build Coastguard WorkerSpecifically, if both GoogleTest and some other code define macro FOO, you can 201*fb1b10abSAndroid Build Coastguard Workeradd 202*fb1b10abSAndroid Build Coastguard Worker 203*fb1b10abSAndroid Build Coastguard Worker -DGTEST_DONT_DEFINE_FOO=1 204*fb1b10abSAndroid Build Coastguard Worker 205*fb1b10abSAndroid Build Coastguard Workerto the compiler flags to tell GoogleTest to change the macro's name from `FOO` 206*fb1b10abSAndroid Build Coastguard Workerto `GTEST_FOO`. Currently `FOO` can be `ASSERT_EQ`, `ASSERT_FALSE`, `ASSERT_GE`, 207*fb1b10abSAndroid Build Coastguard Worker`ASSERT_GT`, `ASSERT_LE`, `ASSERT_LT`, `ASSERT_NE`, `ASSERT_TRUE`, 208*fb1b10abSAndroid Build Coastguard Worker`EXPECT_FALSE`, `EXPECT_TRUE`, `FAIL`, `SUCCEED`, `TEST`, or `TEST_F`. For 209*fb1b10abSAndroid Build Coastguard Workerexample, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write 210*fb1b10abSAndroid Build Coastguard Worker 211*fb1b10abSAndroid Build Coastguard Worker GTEST_TEST(SomeTest, DoesThis) { ... } 212*fb1b10abSAndroid Build Coastguard Worker 213*fb1b10abSAndroid Build Coastguard Workerinstead of 214*fb1b10abSAndroid Build Coastguard Worker 215*fb1b10abSAndroid Build Coastguard Worker TEST(SomeTest, DoesThis) { ... } 216*fb1b10abSAndroid Build Coastguard Worker 217*fb1b10abSAndroid Build Coastguard Workerin order to define a test. 218