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