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