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