You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.9 KiB

  1. ## Using GoogleTest from various build systems
  2. GoogleTest comes with pkg-config files that can be used to determine all
  3. necessary flags for compiling and linking to GoogleTest (and GoogleMock).
  4. Pkg-config is a standardised plain-text format containing
  5. * the includedir (-I) path
  6. * necessary macro (-D) definitions
  7. * further required flags (-pthread)
  8. * the library (-L) path
  9. * the library (-l) to link to
  10. All current build systems support pkg-config in one way or another. For all
  11. examples here we assume you want to compile the sample
  12. `samples/sample3_unittest.cc`.
  13. ### CMake
  14. Using `pkg-config` in CMake is fairly easy:
  15. ```cmake
  16. cmake_minimum_required(VERSION 3.0)
  17. cmake_policy(SET CMP0048 NEW)
  18. project(my_gtest_pkgconfig VERSION 0.0.1 LANGUAGES CXX)
  19. find_package(PkgConfig)
  20. pkg_search_module(GTEST REQUIRED gtest_main)
  21. add_executable(testapp samples/sample3_unittest.cc)
  22. target_link_libraries(testapp ${GTEST_LDFLAGS})
  23. target_compile_options(testapp PUBLIC ${GTEST_CFLAGS})
  24. include(CTest)
  25. add_test(first_and_only_test testapp)
  26. ```
  27. It is generally recommended that you use `target_compile_options` + `_CFLAGS`
  28. over `target_include_directories` + `_INCLUDE_DIRS` as the former includes not
  29. just -I flags (GoogleTest might require a macro indicating to internal headers
  30. that all libraries have been compiled with threading enabled. In addition,
  31. GoogleTest might also require `-pthread` in the compiling step, and as such
  32. splitting the pkg-config `Cflags` variable into include dirs and macros for
  33. `target_compile_definitions()` might still miss this). The same recommendation
  34. goes for using `_LDFLAGS` over the more commonplace `_LIBRARIES`, which happens
  35. to discard `-L` flags and `-pthread`.
  36. ### Autotools
  37. Finding GoogleTest in Autoconf and using it from Automake is also fairly easy:
  38. In your `configure.ac`:
  39. ```
  40. AC_PREREQ([2.69])
  41. AC_INIT([my_gtest_pkgconfig], [0.0.1])
  42. AC_CONFIG_SRCDIR([samples/sample3_unittest.cc])
  43. AC_PROG_CXX
  44. PKG_CHECK_MODULES([GTEST], [gtest_main])
  45. AM_INIT_AUTOMAKE([foreign subdir-objects])
  46. AC_CONFIG_FILES([Makefile])
  47. AC_OUTPUT
  48. ```
  49. and in your `Makefile.am`:
  50. ```
  51. check_PROGRAMS = testapp
  52. TESTS = $(check_PROGRAMS)
  53. testapp_SOURCES = samples/sample3_unittest.cc
  54. testapp_CXXFLAGS = $(GTEST_CFLAGS)
  55. testapp_LDADD = $(GTEST_LIBS)
  56. ```
  57. ### Meson
  58. Meson natively uses pkgconfig to query dependencies:
  59. ```
  60. project('my_gtest_pkgconfig', 'cpp', version : '0.0.1')
  61. gtest_dep = dependency('gtest_main')
  62. testapp = executable(
  63. 'testapp',
  64. files(['samples/sample3_unittest.cc']),
  65. dependencies : gtest_dep,
  66. install : false)
  67. test('first_and_only_test', testapp)
  68. ```
  69. ### Plain Makefiles
  70. Since `pkg-config` is a small Unix command-line utility, it can be used in
  71. handwritten `Makefile`s too:
  72. ```makefile
  73. GTEST_CFLAGS = `pkg-config --cflags gtest_main`
  74. GTEST_LIBS = `pkg-config --libs gtest_main`
  75. .PHONY: tests all
  76. tests: all
  77. ./testapp
  78. all: testapp
  79. testapp: testapp.o
  80. $(CXX) $(CXXFLAGS) $(LDFLAGS) $< -o $@ $(GTEST_LIBS)
  81. testapp.o: samples/sample3_unittest.cc
  82. $(CXX) $(CPPFLAGS) $(CXXFLAGS) $< -c -o $@ $(GTEST_CFLAGS)
  83. ```
  84. ### Help! pkg-config can't find GoogleTest!
  85. Let's say you have a `CMakeLists.txt` along the lines of the one in this
  86. tutorial and you try to run `cmake`. It is very possible that you get a failure
  87. along the lines of:
  88. ```
  89. -- Checking for one of the modules 'gtest_main'
  90. CMake Error at /usr/share/cmake/Modules/FindPkgConfig.cmake:640 (message):
  91. None of the required 'gtest_main' found
  92. ```
  93. These failures are common if you installed GoogleTest yourself and have not
  94. sourced it from a distro or other package manager. If so, you need to tell
  95. pkg-config where it can find the `.pc` files containing the information. Say you
  96. installed GoogleTest to `/usr/local`, then it might be that the `.pc` files are
  97. installed under `/usr/local/lib64/pkgconfig`. If you set
  98. ```
  99. export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig
  100. ```
  101. pkg-config will also try to look in `PKG_CONFIG_PATH` to find `gtest_main.pc`.