pkgconfig.md (3984B)
1 ## Using GoogleTest from various build systems 2 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 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 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`. 16 17 ### CMake 18 19 Using `pkg-config` in CMake is fairly easy: 20 21 ```cmake 22 cmake_minimum_required(VERSION 3.0) 23 24 cmake_policy(SET CMP0048 NEW) 25 project(my_gtest_pkgconfig VERSION 0.0.1 LANGUAGES CXX) 26 27 find_package(PkgConfig) 28 pkg_search_module(GTEST REQUIRED gtest_main) 29 30 add_executable(testapp samples/sample3_unittest.cc) 31 target_link_libraries(testapp ${GTEST_LDFLAGS}) 32 target_compile_options(testapp PUBLIC ${GTEST_CFLAGS}) 33 34 include(CTest) 35 add_test(first_and_only_test testapp) 36 ``` 37 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`. 47 48 ### Autotools 49 50 Finding GoogleTest in Autoconf and using it from Automake is also fairly easy: 51 52 In your `configure.ac`: 53 54 ``` 55 AC_PREREQ([2.69]) 56 AC_INIT([my_gtest_pkgconfig], [0.0.1]) 57 AC_CONFIG_SRCDIR([samples/sample3_unittest.cc]) 58 AC_PROG_CXX 59 60 PKG_CHECK_MODULES([GTEST], [gtest_main]) 61 62 AM_INIT_AUTOMAKE([foreign subdir-objects]) 63 AC_CONFIG_FILES([Makefile]) 64 AC_OUTPUT 65 ``` 66 67 and in your `Makefile.am`: 68 69 ``` 70 check_PROGRAMS = testapp 71 TESTS = $(check_PROGRAMS) 72 73 testapp_SOURCES = samples/sample3_unittest.cc 74 testapp_CXXFLAGS = $(GTEST_CFLAGS) 75 testapp_LDADD = $(GTEST_LIBS) 76 ``` 77 78 ### Meson 79 80 Meson natively uses pkgconfig to query dependencies: 81 82 ``` 83 project('my_gtest_pkgconfig', 'cpp', version : '0.0.1') 84 85 gtest_dep = dependency('gtest_main') 86 87 testapp = executable( 88 'testapp', 89 files(['samples/sample3_unittest.cc']), 90 dependencies : gtest_dep, 91 install : false) 92 93 test('first_and_only_test', testapp) 94 ``` 95 96 ### Plain Makefiles 97 98 Since `pkg-config` is a small Unix command-line utility, it can be used in 99 handwritten `Makefile`s too: 100 101 ```makefile 102 GTEST_CFLAGS = `pkg-config --cflags gtest_main` 103 GTEST_LIBS = `pkg-config --libs gtest_main` 104 105 .PHONY: tests all 106 107 tests: all 108 ./testapp 109 110 all: testapp 111 112 testapp: testapp.o 113 $(CXX) $(CXXFLAGS) $(LDFLAGS) $< -o $@ $(GTEST_LIBS) 114 115 testapp.o: samples/sample3_unittest.cc 116 $(CXX) $(CPPFLAGS) $(CXXFLAGS) $< -c -o $@ $(GTEST_CFLAGS) 117 ``` 118 119 ### Help! pkg-config can't find GoogleTest! 120 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 123 along the lines of: 124 125 ``` 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 129 ``` 130 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 136 137 ``` 138 export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig 139 ``` 140 141 pkg-config will also try to look in `PKG_CONFIG_PATH` to find `gtest_main.pc`.