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