| 1 | |
| 2 | ### Generic Build Instructions ### |
| 3 | |
| 4 | #### Setup #### |
| 5 | |
| 6 | To build Google Test and your tests that use it, you need to tell your |
| 7 | build system where to find its headers and source files. The exact |
| 8 | way to do it depends on which build system you use, and is usually |
| 9 | straightforward. |
| 10 | |
| 11 | #### Build #### |
| 12 | |
| 13 | Suppose you put Google Test in directory `${GTEST_DIR}`. To build it, |
| 14 | create a library build target (or a project as called by Visual Studio |
| 15 | and Xcode) to compile |
| 16 | |
| 17 | ```bash |
| 18 | ${GTEST_DIR}/src/gtest-all.cc |
| 19 | ``` |
| 20 | |
| 21 | with `${GTEST_DIR}/include` in the system header search path and `${GTEST_DIR}` |
| 22 | in the normal header search path. Assuming a Linux-like system and gcc, |
| 23 | something like the following will do: |
| 24 | |
| 25 | ```bash |
| 26 | g++ -isystem ${GTEST_DIR}/include -I${GTEST_DIR} \ |
| 27 | -pthread -c ${GTEST_DIR}/src/gtest-all.cc |
| 28 | ar -rv libgtest.a gtest-all.o |
| 29 | ``` |
| 30 | |
| 31 | (We need `-pthread` as Google Test uses threads.) |
| 32 | |
| 33 | Next, you should compile your test source file with |
| 34 | `${GTEST_DIR}/include` in the system header search path, and link it |
| 35 | with gtest and any other necessary libraries: |
| 36 | |
| 37 | ```bash |
| 38 | g++ -isystem ${GTEST_DIR}/include -pthread path/to/your_test.cc libgtest.a \ |
| 39 | -o your_test |
| 40 | ``` |
| 41 | |
| 42 | As an example, the make/ directory contains a Makefile that you can |
| 43 | use to build Google Test on systems where GNU make is available |
| 44 | (e.g. Linux, Mac OS X, and Cygwin). It doesn't try to build Google |
| 45 | Test's own tests. Instead, it just builds the Google Test library and |
| 46 | a sample test. You can use it as a starting point for your own build |
| 47 | script. |
| 48 | |
| 49 | If the default settings are correct for your environment, the |
| 50 | following commands should succeed: |
| 51 | |
| 52 | ```bash |
| 53 | cd ${GTEST_DIR}/make |
| 54 | make |
| 55 | ./sample1_unittest |
| 56 | ``` |
| 57 | |
| 58 | If you see errors, try to tweak the contents of `make/Makefile` to make |
| 59 | them go away. There are instructions in `make/Makefile` on how to do |
| 60 | it. |
| 61 | |
| 62 | ### Using CMake ### |
| 63 | |
| 64 | Google Test comes with a CMake build script ( |
| 65 | [CMakeLists.txt](CMakeLists.txt)) that can be used on a wide range of platforms ("C" stands for |
| 66 | cross-platform.). If you don't have CMake installed already, you can |
| 67 | download it for free from <http://www.cmake.org/>. |
| 68 | |
| 69 | CMake works by generating native makefiles or build projects that can |
| 70 | be used in the compiler environment of your choice. The typical |
| 71 | workflow starts with: |
| 72 | |
| 73 | ```bash |
| 74 | mkdir mybuild # Create a directory to hold the build output. |
| 75 | cd mybuild |
| 76 | cmake ${GTEST_DIR} # Generate native build scripts. |
| 77 | ``` |
| 78 | |
| 79 | If you want to build Google Test's samples, you should replace the |
| 80 | last command with |
| 81 | |
| 82 | ```bash |
| 83 | cmake -Dgtest_build_samples=ON ${GTEST_DIR} |
| 84 | ``` |
| 85 | |
| 86 | If you are on a \*nix system, you should now see a Makefile in the |
| 87 | current directory. Just type 'make' to build gtest. |
| 88 | |
| 89 | If you use Windows and have Visual Studio installed, a `gtest.sln` file |
| 90 | and several `.vcproj` files will be created. You can then build them |
| 91 | using Visual Studio. |
| 92 | |
| 93 | On Mac OS X with Xcode installed, a `.xcodeproj` file will be generated. |
| 94 | |
| 95 | ### Legacy Build Scripts ### |
| 96 | |
| 97 | Before settling on CMake, we have been providing hand-maintained build |
| 98 | projects/scripts for Visual Studio, Xcode, and Autotools. While we |
| 99 | continue to provide them for convenience, they are not actively |
| 100 | maintained any more. We highly recommend that you follow the |
| 101 | instructions in the previous two sections to integrate Google Test |
| 102 | with your existing build system. |
| 103 | |
| 104 | If you still need to use the legacy build scripts, here's how: |
| 105 | |
| 106 | The msvc\ folder contains two solutions with Visual C++ projects. |
| 107 | Open the `gtest.sln` or `gtest-md.sln` file using Visual Studio, and you |
| 108 | are ready to build Google Test the same way you build any Visual |
| 109 | Studio project. Files that have names ending with -md use DLL |
| 110 | versions of Microsoft runtime libraries (the /MD or the /MDd compiler |
| 111 | option). Files without that suffix use static versions of the runtime |
| 112 | libraries (the /MT or the /MTd option). Please note that one must use |
| 113 | the same option to compile both gtest and the test code. If you use |
| 114 | Visual Studio 2005 or above, we recommend the -md version as /MD is |
| 115 | the default for new projects in these versions of Visual Studio. |
| 116 | |
| 117 | On Mac OS X, open the `gtest.xcodeproj` in the `xcode/` folder using |
| 118 | Xcode. Build the "gtest" target. The universal binary framework will |
| 119 | end up in your selected build directory (selected in the Xcode |
| 120 | "Preferences..." -> "Building" pane and defaults to xcode/build). |
| 121 | Alternatively, at the command line, enter: |
| 122 | |
| 123 | ```bash |
| 124 | xcodebuild |
| 125 | ``` |
| 126 | |
| 127 | This will build the "Release" configuration of gtest.framework in your |
| 128 | default build location. See the "xcodebuild" man page for more |
| 129 | information about building different configurations and building in |
| 130 | different locations. |
| 131 | |
| 132 | If you wish to use the Google Test Xcode project with Xcode 4.x and |
| 133 | above, you need to either: |
| 134 | |
| 135 | * update the SDK configuration options in xcode/Config/General.xconfig. |
| 136 | Comment options `SDKROOT`, `MACOS_DEPLOYMENT_TARGET`, and `GCC_VERSION`. If |
| 137 | you choose this route you lose the ability to target earlier versions |
| 138 | of MacOS X. |
| 139 | * Install an SDK for an earlier version. This doesn't appear to be |
| 140 | supported by Apple, but has been reported to work |
| 141 | (http://stackoverflow.com/questions/5378518). |
| 142 | |
| 143 | ### Tweaking Google Test ### |
| 144 | |
| 145 | Google Test can be used in diverse environments. The default |
| 146 | configuration may not work (or may not work well) out of the box in |
| 147 | some environments. However, you can easily tweak Google Test by |
| 148 | defining control macros on the compiler command line. Generally, |
| 149 | these macros are named like `GTEST_XYZ` and you define them to either 1 |
| 150 | or 0 to enable or disable a certain feature. |
| 151 | |
| 152 | We list the most frequently used macros below. For a complete list, |
| 153 | see file [include/gtest/internal/gtest-port.h](include/gtest/internal/gtest-port.h). |
| 154 | |
| 155 | ### Choosing a TR1 Tuple Library ### |
| 156 | |
| 157 | Some Google Test features require the C++ Technical Report 1 (TR1) |
| 158 | tuple library, which is not yet available with all compilers. The |
| 159 | good news is that Google Test implements a subset of TR1 tuple that's |
| 160 | enough for its own need, and will automatically use this when the |
| 161 | compiler doesn't provide TR1 tuple. |
| 162 | |
| 163 | Usually you don't need to care about which tuple library Google Test |
| 164 | uses. However, if your project already uses TR1 tuple, you need to |
| 165 | tell Google Test to use the same TR1 tuple library the rest of your |
| 166 | project uses, or the two tuple implementations will clash. To do |
| 167 | that, add |
| 168 | |
| 169 | ```bash |
| 170 | -DGTEST_USE_OWN_TR1_TUPLE=0 |
| 171 | ``` |
| 172 | |
| 173 | to the compiler flags while compiling Google Test and your tests. If |
| 174 | you want to force Google Test to use its own tuple library, just add |
| 175 | |
| 176 | ```bash |
| 177 | -DGTEST_USE_OWN_TR1_TUPLE=1 |
| 178 | ``` |
| 179 | |
| 180 | to the compiler flags instead. |
| 181 | |
| 182 | If you don't want Google Test to use tuple at all, add |
| 183 | |
| 184 | ```bash |
| 185 | -DGTEST_HAS_TR1_TUPLE=0 |
| 186 | ``` |
| 187 | |
| 188 | and all features using tuple will be disabled. |
| 189 | |
| 190 | ### Multi-threaded Tests ### |
| 191 | |
| 192 | Google Test is thread-safe where the pthread library is available. |
| 193 | After `#include "gtest/gtest.h"`, you can check the `GTEST_IS_THREADSAFE` |
| 194 | macro to see whether this is the case (yes if the macro is `#defined` to |
| 195 | 1, no if it's undefined.). |
| 196 | |
| 197 | If Google Test doesn't correctly detect whether pthread is available |
| 198 | in your environment, you can force it with |
| 199 | |
| 200 | ```bash |
| 201 | -DGTEST_HAS_PTHREAD=1 |
| 202 | ``` |
| 203 | |
| 204 | or |
| 205 | |
| 206 | ```bash |
| 207 | -DGTEST_HAS_PTHREAD=0 |
| 208 | ``` |
| 209 | |
| 210 | When Google Test uses pthread, you may need to add flags to your |
| 211 | compiler and/or linker to select the pthread library, or you'll get |
| 212 | link errors. If you use the CMake script or the deprecated Autotools |
| 213 | script, this is taken care of for you. If you use your own build |
| 214 | script, you'll need to read your compiler and linker's manual to |
| 215 | figure out what flags to add. |
| 216 | |
| 217 | ### As a Shared Library (DLL) ### |
| 218 | |
| 219 | Google Test is compact, so most users can build and link it as a |
| 220 | static library for the simplicity. You can choose to use Google Test |
| 221 | as a shared library (known as a DLL on Windows) if you prefer. |
| 222 | |
| 223 | To compile *gtest* as a shared library, add |
| 224 | |
| 225 | ```bash |
| 226 | -DGTEST_CREATE_SHARED_LIBRARY=1 |
| 227 | ``` |
| 228 | |
| 229 | to the compiler flags. You'll also need to tell the linker to produce |
| 230 | a shared library instead - consult your linker's manual for how to do |
| 231 | it. |
| 232 | |
| 233 | To compile your *tests* that use the gtest shared library, add |
| 234 | |
| 235 | ```bash |
| 236 | -DGTEST_LINKED_AS_SHARED_LIBRARY=1 |
| 237 | ``` |
| 238 | |
| 239 | to the compiler flags. |
| 240 | |
| 241 | Note: while the above steps aren't technically necessary today when |
| 242 | using some compilers (e.g. GCC), they may become necessary in the |
| 243 | future, if we decide to improve the speed of loading the library (see |
| 244 | <http://gcc.gnu.org/wiki/Visibility> for details). Therefore you are |
| 245 | recommended to always add the above flags when using Google Test as a |
| 246 | shared library. Otherwise a future release of Google Test may break |
| 247 | your build script. |
| 248 | |
| 249 | ### Avoiding Macro Name Clashes ### |
| 250 | |
| 251 | In C++, macros don't obey namespaces. Therefore two libraries that |
| 252 | both define a macro of the same name will clash if you #include both |
| 253 | definitions. In case a Google Test macro clashes with another |
| 254 | library, you can force Google Test to rename its macro to avoid the |
| 255 | conflict. |
| 256 | |
| 257 | Specifically, if both Google Test and some other code define macro |
| 258 | FOO, you can add |
| 259 | |
| 260 | ```bash |
| 261 | -DGTEST_DONT_DEFINE_FOO=1 |
| 262 | ``` |
| 263 | |
| 264 | to the compiler flags to tell Google Test to change the macro's name |
| 265 | from `FOO` to `GTEST_FOO`. Currently `FOO` can be `FAIL`, `SUCCEED`, |
| 266 | or `TEST`. For example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll |
| 267 | need to write |
| 268 | |
| 269 | ```c++ |
| 270 | GTEST_TEST(SomeTest, DoesThis) { ... } |
| 271 | ``` |
| 272 | |
| 273 | instead of |
| 274 | |
| 275 | ```c++ |
| 276 | TEST(SomeTest, DoesThis) { ... } |
| 277 | ``` |
| 278 | |
| 279 | in order to define a test. |
| 280 | |
| 281 | ## Developing Google Test ## |
| 282 | |
| 283 | This section discusses how to make your own changes to Google Test. |
| 284 | |
| 285 | ### Testing Google Test Itself ### |
| 286 | |
| 287 | To make sure your changes work as intended and don't break existing |
| 288 | functionality, you'll want to compile and run Google Test's own tests. |
| 289 | For that you can use CMake: |
| 290 | |
| 291 | ```bash |
| 292 | mkdir mybuild |
| 293 | cd mybuild |
| 294 | cmake -Dgtest_build_tests=ON ${GTEST_DIR} |
| 295 | ``` |
| 296 | |
| 297 | Make sure you have Python installed, as some of Google Test's tests |
| 298 | are written in Python. If the cmake command complains about not being |
| 299 | able to find Python (`Could NOT find PythonInterp (missing: |
| 300 | PYTHON_EXECUTABLE)`), try telling it explicitly where your Python |
| 301 | executable can be found: |
| 302 | |
| 303 | ```bash |
| 304 | cmake -DPYTHON_EXECUTABLE=path/to/python -Dgtest_build_tests=ON ${GTEST_DIR} |
| 305 | ``` |
| 306 | |
| 307 | Next, you can build Google Test and all of its own tests. On \*nix, |
| 308 | this is usually done by 'make'. To run the tests, do |
| 309 | |
| 310 | ```bash |
| 311 | make test |
| 312 | ``` |
| 313 | |
| 314 | All tests should pass. |
| 315 | |
| 316 | Normally you don't need to worry about regenerating the source files, |
| 317 | unless you need to modify them. In that case, you should modify the |
| 318 | corresponding .pump files instead and run the pump.py Python script to |
| 319 | regenerate them. You can find pump.py in the [scripts/](scripts/) directory. |
| 320 | Read the [Pump manual](docs/PumpManual.md) for how to use it. |