25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 14 KiB

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