Project: VoltDB (App Definition and Development / Database)
File: Pkgconfig.md

GoogleTest comes with pkg-config files that can be used to determine all necessary flags for compiling and linking to GoogleTest (and GoogleMock). Pkg-config is a standardised plain-text format containing the includedir (-I) path necessary macro (-D) definitions further required flags (-pthread) the library (-L) path the library (-l) to link to All current build systems support pkg-config in one way or another. For all examples here we assume you want to compile the sample `samples/sample3_unittest.cc`. Using `pkg-config` in CMake is fairly easy: ``` cmake cmakeminimumrequired(VERSION 3.0) cmake_policy(SET CMP0048 NEW) project(mygtestpkgconfig VERSION 0.0.1 LANGUAGES CXX) find_package(PkgConfig) pkgsearchmodule(GTEST REQUIRED gtest_main) addexecutable(testapp samples/sample3unittest.cc) targetlinklibraries(testapp ${GTEST_LDFLAGS}) targetcompileoptions(testapp PUBLIC ${GTEST_CFLAGS}) include(CTest) addtest(firstandonlytest testapp) ``` It is generally recommended that you use `targetcompileoptions` + `_CFLAGS` over `targetincludedirectories` + `INCLUDEDIRS` as the former includes not just -I flags (GoogleTest might require a macro indicating to internal headers that all libraries have been compiled with threading enabled. In addition, GoogleTest might also require `-pthread` in the compiling step, and as such splitting the pkg-config `Cflags` variable into include dirs and macros for `targetcompiledefinitions()` might still miss this). The same recommendation goes for using `LDFLAGS` over the more commonplace `LIBRARIES`, which happens to discard `-L` flags and `-pthread`. Finding GoogleTest in Autoconf and using it from Automake is also fairly easy: In your `configure.ac`: ``` AC_PREREQ([2.69]) ACINIT([mygtest_pkgconfig], [0.0.1]) ACCONFIGSRCDIR([samples/sample3_unittest.cc]) ACPROGCXX PKGCHECKMODULES([GTEST], [gtest_main]) AMINITAUTOMAKE([foreign subdir-objects]) ACCONFIGFILES([Makefile]) AC_OUTPUT ``` and in your `Makefile.am`: ``` check_PROGRAMS = testapp TESTS = $(check_PROGRAMS) testappSOURCES = samples/sample3unittest.cc testappCXXFLAGS = $(GTESTCFLAGS) testappLDADD = $(GTESTLIBS) ``` Meson natively uses pkgconfig to query dependencies: ``` project('mygtestpkgconfig', 'cpp', version : '0.0.1') gtestdep = dependency('gtestmain') testapp = executable( 'testapp', files(['samples/sample3_unittest.cc']), dependencies : gtest_dep, install : false) test('firstandonly_test', testapp) ``` Since `pkg-config` is a small Unix command-line utility, it can be used in handwritten `Makefile`s too: ``` Makefile GTESTCFLAGS = `pkg-config --cflags gtestmain` GTESTLIBS = `pkg-config --libs gtestmain` .PHONY: tests all tests: all ./testapp all: testapp testapp: testapp.o $(CXX) $(CXXFLAGS) $(LDFLAGS) $< -o $@ $(GTEST_LIBS) testapp.o: samples/sample3_unittest.cc $(CXX) $(CPPFLAGS) $(CXXFLAGS) $< -c -o $@ $(GTEST_CFLAGS) ``` Let's say you have a `CMakeLists.txt` along the lines of the one in this tutorial and you try to run `cmake`. It is very possible that you get a failure along the lines of: ``` -- Checking for one of the modules 'gtest_main' CMake Error at /usr/share/cmake/Modules/FindPkgConfig.cmake:640 (message): None of the required 'gtest_main' found ``` These failures are common if you installed GoogleTest yourself and have not sourced it from a distro or other package manager. If so, you need to tell pkg-config where it can find the `.pc` files containing the information. Say you installed GoogleTest to `/usr/local`, then it might be that the `.pc` files are installed under `/usr/local/lib64/pkgconfig`. If you set ``` export PKGCONFIGPATH=/usr/local/lib64/pkgconfig ``` pkg-config will also try to look in `PKGCONFIGPATH` to find `gtest_main.pc`.