xref: /freebsd/contrib/googletest/docs/pkgconfig.md (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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
22find_package(PkgConfig)
23pkg_search_module(GTEST REQUIRED gtest_main)
24
25add_executable(testapp)
26target_sources(testapp PRIVATE samples/sample3_unittest.cc)
27target_link_libraries(testapp PRIVATE ${GTEST_LDFLAGS})
28target_compile_options(testapp PRIVATE ${GTEST_CFLAGS})
29
30enable_testing()
31add_test(first_and_only_test testapp)
32```
33
34It is generally recommended that you use `target_compile_options` + `_CFLAGS`
35over `target_include_directories` + `_INCLUDE_DIRS` as the former includes not
36just -I flags (GoogleTest might require a macro indicating to internal headers
37that all libraries have been compiled with threading enabled. In addition,
38GoogleTest might also require `-pthread` in the compiling step, and as such
39splitting the pkg-config `Cflags` variable into include dirs and macros for
40`target_compile_definitions()` might still miss this). The same recommendation
41goes for using `_LDFLAGS` over the more commonplace `_LIBRARIES`, which happens
42to discard `-L` flags and `-pthread`.
43
44### Help! pkg-config can't find GoogleTest!
45
46Let's say you have a `CMakeLists.txt` along the lines of the one in this
47tutorial and you try to run `cmake`. It is very possible that you get a failure
48along the lines of:
49
50```
51-- Checking for one of the modules 'gtest_main'
52CMake Error at /usr/share/cmake/Modules/FindPkgConfig.cmake:640 (message):
53  None of the required 'gtest_main' found
54```
55
56These failures are common if you installed GoogleTest yourself and have not
57sourced it from a distro or other package manager. If so, you need to tell
58pkg-config where it can find the `.pc` files containing the information. Say you
59installed GoogleTest to `/usr/local`, then it might be that the `.pc` files are
60installed under `/usr/local/lib64/pkgconfig`. If you set
61
62```
63export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig
64```
65
66pkg-config will also try to look in `PKG_CONFIG_PATH` to find `gtest_main.pc`.
67
68### Using pkg-config in a cross-compilation setting
69
70Pkg-config can be used in a cross-compilation setting too. To do this, let's
71assume the final prefix of the cross-compiled installation will be `/usr`, and
72your sysroot is `/home/MYUSER/sysroot`. Configure and install GTest using
73
74```
75mkdir build && cmake -DCMAKE_INSTALL_PREFIX=/usr ..
76```
77
78Install into the sysroot using `DESTDIR`:
79
80```
81make -j install DESTDIR=/home/MYUSER/sysroot
82```
83
84Before we continue, it is recommended to **always** define the following two
85variables for pkg-config in a cross-compilation setting:
86
87```
88export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=yes
89export PKG_CONFIG_ALLOW_SYSTEM_LIBS=yes
90```
91
92otherwise `pkg-config` will filter `-I` and `-L` flags against standard prefixes
93such as `/usr` (see https://bugs.freedesktop.org/show_bug.cgi?id=28264#c3 for
94reasons why this stripping needs to occur usually).
95
96If you look at the generated pkg-config file, it will look something like
97
98```
99libdir=/usr/lib64
100includedir=/usr/include
101
102Name: gtest
103Description: GoogleTest (without main() function)
104Version: 1.11.0
105URL: https://github.com/google/googletest
106Libs: -L${libdir} -lgtest -lpthread
107Cflags: -I${includedir} -DGTEST_HAS_PTHREAD=1 -lpthread
108```
109
110Notice that the sysroot is not included in `libdir` and `includedir`! If you try
111to run `pkg-config` with the correct
112`PKG_CONFIG_LIBDIR=/home/MYUSER/sysroot/usr/lib64/pkgconfig` against this `.pc`
113file, you will get
114
115```
116$ pkg-config --cflags gtest
117-DGTEST_HAS_PTHREAD=1 -lpthread -I/usr/include
118$ pkg-config --libs gtest
119-L/usr/lib64 -lgtest -lpthread
120```
121
122which is obviously wrong and points to the `CBUILD` and not `CHOST` root. In
123order to use this in a cross-compilation setting, we need to tell pkg-config to
124inject the actual sysroot into `-I` and `-L` variables. Let us now tell
125pkg-config about the actual sysroot
126
127```
128export PKG_CONFIG_DIR=
129export PKG_CONFIG_SYSROOT_DIR=/home/MYUSER/sysroot
130export PKG_CONFIG_LIBDIR=${PKG_CONFIG_SYSROOT_DIR}/usr/lib64/pkgconfig
131```
132
133and running `pkg-config` again we get
134
135```
136$ pkg-config --cflags gtest
137-DGTEST_HAS_PTHREAD=1 -lpthread -I/home/MYUSER/sysroot/usr/include
138$ pkg-config --libs gtest
139-L/home/MYUSER/sysroot/usr/lib64 -lgtest -lpthread
140```
141
142which contains the correct sysroot now. For a more comprehensive guide to also
143including `${CHOST}` in build system calls, see the excellent tutorial by Diego
144Elio Pettenò: <https://autotools.io/pkgconfig/cross-compiling.html>
145