54913
|
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
|
|
10 |
|
|
11 |
Suppose you put Google Test in directory `${GTEST_DIR}`. To build it, create a
|
|
12 |
library build target (or a project as called by Visual Studio and Xcode) to
|
|
13 |
compile
|
|
14 |
|
|
15 |
${GTEST_DIR}/src/gtest-all.cc
|
|
16 |
|
|
17 |
with `${GTEST_DIR}/include` in the system header search path and `${GTEST_DIR}`
|
|
18 |
in the normal header search path. Assuming a Linux-like system and gcc,
|
|
19 |
something like the following will do:
|
|
20 |
|
|
21 |
g++ -isystem ${GTEST_DIR}/include -I${GTEST_DIR} \
|
|
22 |
-pthread -c ${GTEST_DIR}/src/gtest-all.cc
|
|
23 |
ar -rv libgtest.a gtest-all.o
|
|
24 |
|
|
25 |
(We need `-pthread` as Google Test uses threads.)
|
|
26 |
|
|
27 |
Next, you should compile your test source file with `${GTEST_DIR}/include` in
|
|
28 |
the system header search path, and link it with gtest and any other necessary
|
|
29 |
libraries:
|
|
30 |
|
|
31 |
g++ -isystem ${GTEST_DIR}/include -pthread path/to/your_test.cc libgtest.a \
|
|
32 |
-o your_test
|
|
33 |
|
|
34 |
As an example, the make/ directory contains a Makefile that you can use to build
|
|
35 |
Google Test on systems where GNU make is available (e.g. Linux, Mac OS X, and
|
|
36 |
Cygwin). It doesn't try to build Google Test's own tests. Instead, it just
|
|
37 |
builds the Google Test library and a sample test. You can use it as a starting
|
|
38 |
point for your own build script.
|
|
39 |
|
|
40 |
If the default settings are correct for your environment, the following commands
|
|
41 |
should succeed:
|
|
42 |
|
|
43 |
cd ${GTEST_DIR}/make
|
|
44 |
make
|
|
45 |
./sample1_unittest
|
|
46 |
|
|
47 |
If you see errors, try to tweak the contents of `make/Makefile` to make them go
|
|
48 |
away. There are instructions in `make/Makefile` on how to do it.
|
|
49 |
|
|
50 |
### Using CMake
|
|
51 |
|
|
52 |
Google Test comes with a CMake build script (
|
|
53 |
[CMakeLists.txt](https://github.com/google/googletest/blob/master/CMakeLists.txt))
|
|
54 |
that can be used on a wide range of platforms ("C" stands for cross-platform.).
|
|
55 |
If you don't have CMake installed already, you can download it for free from
|
|
56 |
<http://www.cmake.org/>.
|
|
57 |
|
|
58 |
CMake works by generating native makefiles or build projects that can be used in
|
|
59 |
the compiler environment of your choice. You can either build Google Test as a
|
|
60 |
standalone project or it can be incorporated into an existing CMake build for
|
|
61 |
another project.
|
|
62 |
|
|
63 |
#### Standalone CMake Project
|
|
64 |
|
|
65 |
When building Google Test as a standalone project, the typical workflow starts
|
|
66 |
with:
|
|
67 |
|
|
68 |
mkdir mybuild # Create a directory to hold the build output.
|
|
69 |
cd mybuild
|
|
70 |
cmake ${GTEST_DIR} # Generate native build scripts.
|
|
71 |
|
|
72 |
If you want to build Google Test's samples, you should replace the last command
|
|
73 |
with
|
|
74 |
|
|
75 |
cmake -Dgtest_build_samples=ON ${GTEST_DIR}
|
|
76 |
|
|
77 |
If you are on a \*nix system, you should now see a Makefile in the current
|
|
78 |
directory. Just type 'make' to build gtest.
|
|
79 |
|
|
80 |
If you use Windows and have Visual Studio installed, a `gtest.sln` file and
|
|
81 |
several `.vcproj` files will be created. You can then build them using Visual
|
|
82 |
Studio.
|
|
83 |
|
|
84 |
On Mac OS X with Xcode installed, a `.xcodeproj` file will be generated.
|
|
85 |
|
|
86 |
#### Incorporating Into An Existing CMake Project
|
|
87 |
|
|
88 |
If you want to use gtest in a project which already uses CMake, then a more
|
|
89 |
robust and flexible approach is to build gtest as part of that project directly.
|
|
90 |
This is done by making the GoogleTest source code available to the main build
|
|
91 |
and adding it using CMake's `add_subdirectory()` command. This has the
|
|
92 |
significant advantage that the same compiler and linker settings are used
|
|
93 |
between gtest and the rest of your project, so issues associated with using
|
|
94 |
incompatible libraries (eg debug/release), etc. are avoided. This is
|
|
95 |
particularly useful on Windows. Making GoogleTest's source code available to the
|
|
96 |
main build can be done a few different ways:
|
|
97 |
|
|
98 |
* Download the GoogleTest source code manually and place it at a known
|
|
99 |
location. This is the least flexible approach and can make it more difficult
|
|
100 |
to use with continuous integration systems, etc.
|
|
101 |
* Embed the GoogleTest source code as a direct copy in the main project's
|
|
102 |
source tree. This is often the simplest approach, but is also the hardest to
|
|
103 |
keep up to date. Some organizations may not permit this method.
|
|
104 |
* Add GoogleTest as a git submodule or equivalent. This may not always be
|
|
105 |
possible or appropriate. Git submodules, for example, have their own set of
|
|
106 |
advantages and drawbacks.
|
|
107 |
* Use CMake to download GoogleTest as part of the build's configure step. This
|
|
108 |
is just a little more complex, but doesn't have the limitations of the other
|
|
109 |
methods.
|
|
110 |
|
|
111 |
The last of the above methods is implemented with a small piece of CMake code in
|
|
112 |
a separate file (e.g. `CMakeLists.txt.in`) which is copied to the build area and
|
|
113 |
then invoked as a sub-build _during the CMake stage_. That directory is then
|
|
114 |
pulled into the main build with `add_subdirectory()`. For example:
|
|
115 |
|
|
116 |
New file `CMakeLists.txt.in`:
|
|
117 |
|
|
118 |
``` cmake
|
|
119 |
cmake_minimum_required(VERSION 2.8.2)
|
|
120 |
|
|
121 |
project(googletest-download NONE)
|
|
122 |
|
|
123 |
include(ExternalProject)
|
|
124 |
ExternalProject_Add(googletest
|
|
125 |
GIT_REPOSITORY https://github.com/google/googletest.git
|
|
126 |
GIT_TAG master
|
|
127 |
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
|
|
128 |
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
|
|
129 |
CONFIGURE_COMMAND ""
|
|
130 |
BUILD_COMMAND ""
|
|
131 |
INSTALL_COMMAND ""
|
|
132 |
TEST_COMMAND ""
|
|
133 |
)
|
|
134 |
```
|
|
135 |
|
|
136 |
Existing build's `CMakeLists.txt`:
|
|
137 |
|
|
138 |
``` cmake
|
|
139 |
# Download and unpack googletest at configure time
|
|
140 |
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
|
|
141 |
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
|
|
142 |
RESULT_VARIABLE result
|
|
143 |
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
|
|
144 |
if(result)
|
|
145 |
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
|
|
146 |
endif()
|
|
147 |
execute_process(COMMAND ${CMAKE_COMMAND} --build .
|
|
148 |
RESULT_VARIABLE result
|
|
149 |
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
|
|
150 |
if(result)
|
|
151 |
message(FATAL_ERROR "Build step for googletest failed: ${result}")
|
|
152 |
endif()
|
|
153 |
|
|
154 |
# Prevent overriding the parent project's compiler/linker
|
|
155 |
# settings on Windows
|
|
156 |
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
|
157 |
|
|
158 |
# Add googletest directly to our build. This defines
|
|
159 |
# the gtest and gtest_main targets.
|
|
160 |
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
|
|
161 |
${CMAKE_BINARY_DIR}/googletest-build
|
|
162 |
EXCLUDE_FROM_ALL)
|
|
163 |
|
|
164 |
# The gtest/gtest_main targets carry header search path
|
|
165 |
# dependencies automatically when using CMake 2.8.11 or
|
|
166 |
# later. Otherwise we have to add them here ourselves.
|
|
167 |
if (CMAKE_VERSION VERSION_LESS 2.8.11)
|
|
168 |
include_directories("${gtest_SOURCE_DIR}/include")
|
|
169 |
endif()
|
|
170 |
|
|
171 |
# Now simply link against gtest or gtest_main as needed. Eg
|
|
172 |
add_executable(example example.cpp)
|
|
173 |
target_link_libraries(example gtest_main)
|
|
174 |
add_test(NAME example_test COMMAND example)
|
|
175 |
```
|
|
176 |
|
|
177 |
Note that this approach requires CMake 2.8.2 or later due to its use of the
|
|
178 |
`ExternalProject_Add()` command. The above technique is discussed in more detail
|
|
179 |
in [this separate article](http://crascit.com/2015/07/25/cmake-gtest/) which
|
|
180 |
also contains a link to a fully generalized implementation of the technique.
|
|
181 |
|
|
182 |
##### Visual Studio Dynamic vs Static Runtimes
|
|
183 |
|
|
184 |
By default, new Visual Studio projects link the C runtimes dynamically but
|
|
185 |
Google Test links them statically. This will generate an error that looks
|
|
186 |
something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch
|
|
187 |
detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value
|
|
188 |
'MDd_DynamicDebug' in main.obj
|
|
189 |
|
|
190 |
Google Test already has a CMake option for this: `gtest_force_shared_crt`
|
|
191 |
|
|
192 |
Enabling this option will make gtest link the runtimes dynamically too, and
|
|
193 |
match the project in which it is included.
|
|
194 |
|
|
195 |
### Legacy Build Scripts
|
|
196 |
|
|
197 |
Before settling on CMake, we have been providing hand-maintained build
|
|
198 |
projects/scripts for Visual Studio, Xcode, and Autotools. While we continue to
|
|
199 |
provide them for convenience, they are not actively maintained any more. We
|
|
200 |
highly recommend that you follow the instructions in the above sections to
|
|
201 |
integrate Google Test with your existing build system.
|
|
202 |
|
|
203 |
If you still need to use the legacy build scripts, here's how:
|
|
204 |
|
|
205 |
The msvc\ folder contains two solutions with Visual C++ projects. Open the
|
|
206 |
`gtest.sln` or `gtest-md.sln` file using Visual Studio, and you are ready to
|
|
207 |
build Google Test the same way you build any Visual Studio project. Files that
|
|
208 |
have names ending with -md use DLL versions of Microsoft runtime libraries (the
|
|
209 |
/MD or the /MDd compiler option). Files without that suffix use static versions
|
|
210 |
of the runtime libraries (the /MT or the /MTd option). Please note that one must
|
|
211 |
use the same option to compile both gtest and the test code. If you use Visual
|
|
212 |
Studio 2005 or above, we recommend the -md version as /MD is the default for new
|
|
213 |
projects in these versions of Visual Studio.
|
|
214 |
|
|
215 |
On Mac OS X, open the `gtest.xcodeproj` in the `xcode/` folder using Xcode.
|
|
216 |
Build the "gtest" target. The universal binary framework will end up in your
|
|
217 |
selected build directory (selected in the Xcode "Preferences..." -> "Building"
|
|
218 |
pane and defaults to xcode/build). Alternatively, at the command line, enter:
|
|
219 |
|
|
220 |
xcodebuild
|
|
221 |
|
|
222 |
This will build the "Release" configuration of gtest.framework in your default
|
|
223 |
build location. See the "xcodebuild" man page for more information about
|
|
224 |
building different configurations and building in different locations.
|
|
225 |
|
|
226 |
If you wish to use the Google Test Xcode project with Xcode 4.x and above, you
|
|
227 |
need to either:
|
|
228 |
|
|
229 |
* update the SDK configuration options in xcode/Config/General.xconfig.
|
|
230 |
Comment options `SDKROOT`, `MACOS_DEPLOYMENT_TARGET`, and `GCC_VERSION`. If
|
|
231 |
you choose this route you lose the ability to target earlier versions of
|
|
232 |
MacOS X.
|
|
233 |
* Install an SDK for an earlier version. This doesn't appear to be supported
|
|
234 |
by Apple, but has been reported to work
|
|
235 |
(http://stackoverflow.com/questions/5378518).
|
|
236 |
|
|
237 |
### Tweaking Google Test
|
|
238 |
|
|
239 |
Google Test can be used in diverse environments. The default configuration may
|
|
240 |
not work (or may not work well) out of the box in some environments. However,
|
|
241 |
you can easily tweak Google Test by defining control macros on the compiler
|
|
242 |
command line. Generally, these macros are named like `GTEST_XYZ` and you define
|
|
243 |
them to either 1 or 0 to enable or disable a certain feature.
|
|
244 |
|
|
245 |
We list the most frequently used macros below. For a complete list, see file
|
|
246 |
[include/gtest/internal/gtest-port.h](https://github.com/google/googletest/blob/master/include/gtest/internal/gtest-port.h).
|
|
247 |
|
|
248 |
### Choosing a TR1 Tuple Library
|
|
249 |
|
|
250 |
Some Google Test features require the C++ Technical Report 1 (TR1) tuple
|
|
251 |
library, which is not yet available with all compilers. The good news is that
|
|
252 |
Google Test implements a subset of TR1 tuple that's enough for its own need, and
|
|
253 |
will automatically use this when the compiler doesn't provide TR1 tuple.
|
|
254 |
|
|
255 |
Usually you don't need to care about which tuple library Google Test uses.
|
|
256 |
However, if your project already uses TR1 tuple, you need to tell Google Test to
|
|
257 |
use the same TR1 tuple library the rest of your project uses, or the two tuple
|
|
258 |
implementations will clash. To do that, add
|
|
259 |
|
|
260 |
-DGTEST_USE_OWN_TR1_TUPLE=0
|
|
261 |
|
|
262 |
to the compiler flags while compiling Google Test and your tests. If you want to
|
|
263 |
force Google Test to use its own tuple library, just add
|
|
264 |
|
|
265 |
-DGTEST_USE_OWN_TR1_TUPLE=1
|
|
266 |
|
|
267 |
to the compiler flags instead.
|
|
268 |
|
|
269 |
If you don't want Google Test to use tuple at all, add
|
|
270 |
|
|
271 |
-DGTEST_HAS_TR1_TUPLE=0
|
|
272 |
|
|
273 |
and all features using tuple will be disabled.
|
|
274 |
|
|
275 |
### Multi-threaded Tests
|
|
276 |
|
|
277 |
Google Test is thread-safe where the pthread library is available. After
|
|
278 |
`#include "gtest/gtest.h"`, you can check the `GTEST_IS_THREADSAFE` macro to see
|
|
279 |
whether this is the case (yes if the macro is `#defined` to 1, no if it's
|
|
280 |
undefined.).
|
|
281 |
|
|
282 |
If Google Test doesn't correctly detect whether pthread is available in your
|
|
283 |
environment, you can force it with
|
|
284 |
|
|
285 |
-DGTEST_HAS_PTHREAD=1
|
|
286 |
|
|
287 |
or
|
|
288 |
|
|
289 |
-DGTEST_HAS_PTHREAD=0
|
|
290 |
|
|
291 |
When Google Test uses pthread, you may need to add flags to your compiler and/or
|
|
292 |
linker to select the pthread library, or you'll get link errors. If you use the
|
|
293 |
CMake script or the deprecated Autotools script, this is taken care of for you.
|
|
294 |
If you use your own build script, you'll need to read your compiler and linker's
|
|
295 |
manual to figure out what flags to add.
|
|
296 |
|
|
297 |
### As a Shared Library (DLL)
|
|
298 |
|
|
299 |
Google Test is compact, so most users can build and link it as a static library
|
|
300 |
for the simplicity. You can choose to use Google Test as a shared library (known
|
|
301 |
as a DLL on Windows) if you prefer.
|
|
302 |
|
|
303 |
To compile *gtest* as a shared library, add
|
|
304 |
|
|
305 |
-DGTEST_CREATE_SHARED_LIBRARY=1
|
|
306 |
|
|
307 |
to the compiler flags. You'll also need to tell the linker to produce a shared
|
|
308 |
library instead - consult your linker's manual for how to do it.
|
|
309 |
|
|
310 |
To compile your *tests* that use the gtest shared library, add
|
|
311 |
|
|
312 |
-DGTEST_LINKED_AS_SHARED_LIBRARY=1
|
|
313 |
|
|
314 |
to the compiler flags.
|
|
315 |
|
|
316 |
Note: while the above steps aren't technically necessary today when using some
|
|
317 |
compilers (e.g. GCC), they may become necessary in the future, if we decide to
|
|
318 |
improve the speed of loading the library (see
|
|
319 |
<http://gcc.gnu.org/wiki/Visibility> for details). Therefore you are recommended
|
|
320 |
to always add the above flags when using Google Test as a shared library.
|
|
321 |
Otherwise a future release of Google Test may break your build script.
|
|
322 |
|
|
323 |
### Avoiding Macro Name Clashes
|
|
324 |
|
|
325 |
In C++, macros don't obey namespaces. Therefore two libraries that both define a
|
|
326 |
macro of the same name will clash if you `#include` both definitions. In case a
|
|
327 |
Google Test macro clashes with another library, you can force Google Test to
|
|
328 |
rename its macro to avoid the conflict.
|
|
329 |
|
|
330 |
Specifically, if both Google Test and some other code define macro FOO, you can
|
|
331 |
add
|
|
332 |
|
|
333 |
-DGTEST_DONT_DEFINE_FOO=1
|
|
334 |
|
|
335 |
to the compiler flags to tell Google Test to change the macro's name from `FOO`
|
|
336 |
to `GTEST_FOO`. Currently `FOO` can be `FAIL`, `SUCCEED`, or `TEST`. For
|
|
337 |
example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write
|
|
338 |
|
|
339 |
GTEST_TEST(SomeTest, DoesThis) { ... }
|
|
340 |
|
|
341 |
instead of
|
|
342 |
|
|
343 |
TEST(SomeTest, DoesThis) { ... }
|
|
344 |
|
|
345 |
in order to define a test.
|