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