LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.9 KiB

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