• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

cmake/H03-May-2022-395359

docs/H03-May-2022-4,1713,248

include/gtest/H03-May-2022-12,4685,921

samples/H03-May-2022-2,111786

scripts/H03-May-2022-3,0872,215

src/H03-May-2022-12,1237,755

test/H03-May-2022-29,82419,550

CONTRIBUTORSH A D02-Nov-20211.4 KiB3937

LICENSEH A D02-Nov-20211.4 KiB2925

README.mdH A D02-Nov-202110.2 KiB263197

README.md

1### Generic Build Instructions
2
3#### Setup
4
5To build GoogleTest 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
11GoogleTest 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 GoogleTest 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 GoogleTest as a standalone project, the typical workflow starts
25with
26
27```
28git clone https://github.com/google/googletest.git -b release-1.10.0
29cd googletest        # Main directory of the cloned repository.
30mkdir build          # Create a directory to hold the build output.
31cd build
32cmake ..             # Generate native build scripts for GoogleTest.
33```
34
35The above command also includes GoogleMock by default. And so, if you want to
36build only GoogleTest, you should replace the last command with
37
38```
39cmake .. -DBUILD_GMOCK=OFF
40```
41
42If you are on a \*nix system, you should now see a Makefile in the current
43directory. Just type `make` to build GoogleTest. And then you can simply install
44GoogleTest if you are a system administrator.
45
46```
47make
48sudo make install    # Install in /usr/local/ by default
49```
50
51If you use Windows and have Visual Studio installed, a `gtest.sln` file and
52several `.vcproj` files will be created. You can then build them using Visual
53Studio.
54
55On Mac OS X with Xcode installed, a `.xcodeproj` file will be generated.
56
57#### Incorporating Into An Existing CMake Project
58
59If you want to use GoogleTest in a project which already uses CMake, the easiest
60way is to get installed libraries and headers.
61
62*   Import GoogleTest by using `find_package` (or `pkg_check_modules`). For
63    example, if `find_package(GTest CONFIG REQUIRED)` is succeed, you can use
64    the libraries as `GTest::gtest`, `GTest::gmock`.
65
66And a more robust and flexible approach is to build GoogleTest as part of that
67project directly. This is done by making the GoogleTest source code available to
68the main build and adding it using CMake's `add_subdirectory()` command. This
69has the significant advantage that the same compiler and linker settings are
70used between GoogleTest and the rest of your project, so issues associated with
71using incompatible libraries (eg debug/release), etc. are avoided. This is
72particularly useful on Windows. Making GoogleTest's source code available to the
73main build can be done a few different ways:
74
75*   Download the GoogleTest source code manually and place it at a known
76    location. This is the least flexible approach and can make it more difficult
77    to use with continuous integration systems, etc.
78*   Embed the GoogleTest source code as a direct copy in the main project's
79    source tree. This is often the simplest approach, but is also the hardest to
80    keep up to date. Some organizations may not permit this method.
81*   Add GoogleTest as a git submodule or equivalent. This may not always be
82    possible or appropriate. Git submodules, for example, have their own set of
83    advantages and drawbacks.
84*   Use CMake to download GoogleTest as part of the build's configure step. This
85    is just a little more complex, but doesn't have the limitations of the other
86    methods.
87
88The last of the above methods is implemented with a small piece of CMake code in
89a separate file (e.g. `CMakeLists.txt.in`) which is copied to the build area and
90then invoked as a sub-build _during the CMake stage_. That directory is then
91pulled into the main build with `add_subdirectory()`. For example:
92
93New file `CMakeLists.txt.in`:
94
95```cmake
96cmake_minimum_required(VERSION 2.8.2)
97
98project(googletest-download NONE)
99
100include(ExternalProject)
101ExternalProject_Add(googletest
102  GIT_REPOSITORY    https://github.com/google/googletest.git
103  GIT_TAG           master
104  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
105  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
106  CONFIGURE_COMMAND ""
107  BUILD_COMMAND     ""
108  INSTALL_COMMAND   ""
109  TEST_COMMAND      ""
110)
111```
112
113Existing build's `CMakeLists.txt`:
114
115```cmake
116# Download and unpack googletest at configure time
117configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
118execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
119  RESULT_VARIABLE result
120  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
121if(result)
122  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
123endif()
124execute_process(COMMAND ${CMAKE_COMMAND} --build .
125  RESULT_VARIABLE result
126  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
127if(result)
128  message(FATAL_ERROR "Build step for googletest failed: ${result}")
129endif()
130
131# Prevent overriding the parent project's compiler/linker
132# settings on Windows
133set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
134
135# Add googletest directly to our build. This defines
136# the gtest and gtest_main targets.
137add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
138                 ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
139                 EXCLUDE_FROM_ALL)
140
141# The gtest/gtest_main targets carry header search path
142# dependencies automatically when using CMake 2.8.11 or
143# later. Otherwise we have to add them here ourselves.
144if (CMAKE_VERSION VERSION_LESS 2.8.11)
145  include_directories("${gtest_SOURCE_DIR}/include")
146endif()
147
148# Now simply link against gtest or gtest_main as needed. Eg
149add_executable(example example.cpp)
150target_link_libraries(example gtest_main)
151add_test(NAME example_test COMMAND example)
152```
153
154Note that this approach requires CMake 2.8.2 or later due to its use of the
155`ExternalProject_Add()` command. The above technique is discussed in more detail
156in [this separate article](http://crascit.com/2015/07/25/cmake-gtest/) which
157also contains a link to a fully generalized implementation of the technique.
158
159##### Visual Studio Dynamic vs Static Runtimes
160
161By default, new Visual Studio projects link the C runtimes dynamically but
162GoogleTest links them statically. This will generate an error that looks
163something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch
164detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value
165'MDd_DynamicDebug' in main.obj
166
167GoogleTest already has a CMake option for this: `gtest_force_shared_crt`
168
169Enabling this option will make gtest link the runtimes dynamically too, and
170match the project in which it is included.
171
172#### C++ Standard Version
173
174An environment that supports C++11 is required in order to successfully build
175GoogleTest. One way to ensure this is to specify the standard in the top-level
176project, for example by using the `set(CMAKE_CXX_STANDARD 11)` command. If this
177is not feasible, for example in a C project using GoogleTest for validation,
178then it can be specified by adding it to the options for cmake via the
179`DCMAKE_CXX_FLAGS` option.
180
181### Tweaking GoogleTest
182
183GoogleTest can be used in diverse environments. The default configuration may
184not work (or may not work well) out of the box in some environments. However,
185you can easily tweak GoogleTest by defining control macros on the compiler
186command line. Generally, these macros are named like `GTEST_XYZ` and you define
187them to either 1 or 0 to enable or disable a certain feature.
188
189We list the most frequently used macros below. For a complete list, see file
190[include/gtest/internal/gtest-port.h](https://github.com/google/googletest/blob/master/googletest/include/gtest/internal/gtest-port.h).
191
192### Multi-threaded Tests
193
194GoogleTest is thread-safe where the pthread library is available. After
195`#include "gtest/gtest.h"`, you can check the
196`GTEST_IS_THREADSAFE` macro to see whether this is the case (yes if the macro is
197`#defined` to 1, no if it's undefined.).
198
199If GoogleTest doesn't correctly detect whether pthread is available in your
200environment, you can force it with
201
202    -DGTEST_HAS_PTHREAD=1
203
204or
205
206    -DGTEST_HAS_PTHREAD=0
207
208When GoogleTest uses pthread, you may need to add flags to your compiler and/or
209linker to select the pthread library, or you'll get link errors. If you use the
210CMake script, this is taken care of for you. If you use your own build script,
211you'll need to read your compiler and linker's manual to figure out what flags
212to add.
213
214### As a Shared Library (DLL)
215
216GoogleTest is compact, so most users can build and link it as a static library
217for the simplicity. You can choose to use GoogleTest as a shared library (known
218as a DLL on Windows) if you prefer.
219
220To compile *gtest* as a shared library, add
221
222    -DGTEST_CREATE_SHARED_LIBRARY=1
223
224to the compiler flags. You'll also need to tell the linker to produce a shared
225library instead - consult your linker's manual for how to do it.
226
227To compile your *tests* that use the gtest shared library, add
228
229    -DGTEST_LINKED_AS_SHARED_LIBRARY=1
230
231to the compiler flags.
232
233Note: while the above steps aren't technically necessary today when using some
234compilers (e.g. GCC), they may become necessary in the future, if we decide to
235improve the speed of loading the library (see
236<http://gcc.gnu.org/wiki/Visibility> for details). Therefore you are recommended
237to always add the above flags when using GoogleTest as a shared library.
238Otherwise a future release of GoogleTest may break your build script.
239
240### Avoiding Macro Name Clashes
241
242In C++, macros don't obey namespaces. Therefore two libraries that both define a
243macro of the same name will clash if you `#include` both definitions. In case a
244GoogleTest macro clashes with another library, you can force GoogleTest to
245rename its macro to avoid the conflict.
246
247Specifically, if both GoogleTest and some other code define macro FOO, you can
248add
249
250    -DGTEST_DONT_DEFINE_FOO=1
251
252to the compiler flags to tell GoogleTest to change the macro's name from `FOO`
253to `GTEST_FOO`. Currently `FOO` can be `FAIL`, `SUCCEED`, or `TEST`. For
254example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write
255
256    GTEST_TEST(SomeTest, DoesThis) { ... }
257
258instead of
259
260    TEST(SomeTest, DoesThis) { ... }
261
262in order to define a test.
263