1 ## Using GoogleTest from various build systems
3 GoogleTest comes with pkg-config files that can be used to determine all
4 necessary flags for compiling and linking to GoogleTest (and GoogleMock).
5 Pkg-config is a standardised plain-text format containing
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
13 All current build systems support pkg-config in one way or another. For all
14 examples here we assume you want to compile the sample
15 `samples/sample3_unittest.cc`.
19 Using `pkg-config` in CMake is fairly easy:
22 cmake_minimum_required(VERSION 3.0)
24 cmake_policy(SET CMP0048 NEW)
25 project(my_gtest_pkgconfig VERSION 0.0.1 LANGUAGES CXX)
27 find_package(PkgConfig)
28 pkg_search_module(GTEST REQUIRED gtest_main)
30 add_executable(testapp samples/sample3_unittest.cc)
31 target_link_libraries(testapp ${GTEST_LDFLAGS})
32 target_compile_options(testapp PUBLIC ${GTEST_CFLAGS})
35 add_test(first_and_only_test testapp)
38 It is generally recommended that you use `target_compile_options` + `_CFLAGS`
39 over `target_include_directories` + `_INCLUDE_DIRS` as the former includes not
40 just -I flags (GoogleTest might require a macro indicating to internal headers
41 that all libraries have been compiled with threading enabled. In addition,
42 GoogleTest might also require `-pthread` in the compiling step, and as such
43 splitting the pkg-config `Cflags` variable into include dirs and macros for
44 `target_compile_definitions()` might still miss this). The same recommendation
45 goes for using `_LDFLAGS` over the more commonplace `_LIBRARIES`, which happens
46 to discard `-L` flags and `-pthread`.
50 Finding GoogleTest in Autoconf and using it from Automake is also fairly easy:
52 In your `configure.ac`:
56 AC_INIT([my_gtest_pkgconfig], [0.0.1])
57 AC_CONFIG_SRCDIR([samples/sample3_unittest.cc])
60 PKG_CHECK_MODULES([GTEST], [gtest_main])
62 AM_INIT_AUTOMAKE([foreign subdir-objects])
63 AC_CONFIG_FILES([Makefile])
67 and in your `Makefile.am`:
70 check_PROGRAMS = testapp
71 TESTS = $(check_PROGRAMS)
73 testapp_SOURCES = samples/sample3_unittest.cc
74 testapp_CXXFLAGS = $(GTEST_CFLAGS)
75 testapp_LDADD = $(GTEST_LIBS)
80 Meson natively uses pkgconfig to query dependencies:
83 project('my_gtest_pkgconfig', 'cpp', version : '0.0.1')
85 gtest_dep = dependency('gtest_main')
89 files(['samples/sample3_unittest.cc']),
90 dependencies : gtest_dep,
93 test('first_and_only_test', testapp)
98 Since `pkg-config` is a small Unix command-line utility, it can be used in
99 handwritten `Makefile`s too:
102 GTEST_CFLAGS = `pkg-config --cflags gtest_main`
103 GTEST_LIBS = `pkg-config --libs gtest_main`
113 $(CXX) $(CXXFLAGS) $(LDFLAGS) $< -o $@ $(GTEST_LIBS)
115 testapp.o: samples/sample3_unittest.cc
116 $(CXX) $(CPPFLAGS) $(CXXFLAGS) $< -c -o $@ $(GTEST_CFLAGS)
119 ### Help! pkg-config can't find GoogleTest!
121 Let's say you have a `CMakeLists.txt` along the lines of the one in this
122 tutorial and you try to run `cmake`. It is very possible that you get a failure
126 -- Checking for one of the modules 'gtest_main'
127 CMake Error at /usr/share/cmake/Modules/FindPkgConfig.cmake:640 (message):
128 None of the required 'gtest_main' found
131 These failures are common if you installed GoogleTest yourself and have not
132 sourced it from a distro or other package manager. If so, you need to tell
133 pkg-config where it can find the `.pc` files containing the information. Say you
134 installed GoogleTest to `/usr/local`, then it might be that the `.pc` files are
135 installed under `/usr/local/lib64/pkgconfig`. If you set
138 export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig
141 pkg-config will also try to look in `PKG_CONFIG_PATH` to find `gtest_main.pc`.