Introduction

備忘録。

googletest は v1.8.1 から CMake、Pkg-config に対応しているが…
ソースからビルドした googletestCMAKE_PREFIX_PATH を使って find_package によって利用しても、 GTest_INCLUDE_DIRS だけ見つからない。

  • GTest_FOUND
  • GTest_VERSION
  • GTEST_LIBRARIES

は見つかる。

GOOGLETEST_INCLUDE_DIRS とか探してみたけどダメ。
そもそも、GTEST_LIBRARIES は接頭辞が大文字で通常のルールに沿ってない。

同じ問題は上がっている。

自分で googletest をビルドした場合がダメなのか…

How to do?

選択肢は

  • ソースからビルド (make install なし)
    • GTEST_INCLUDE_DIRS が見つからないのは変わらない
  • apt-get install libgtest-dev
    • GTEST_INCLUDE_DIRS は見つかるが、利用できるバージョンが固定
  • ソースからビルド (make install あり)
    • GTEST_INCLUDE_DIRS が見つからないのは変わらない
    • /usr/local/include/gtest にインストールされるので自動的に見つかるがシステムに入るのは納得できない
  • FetchContent_Declare
    • 公式の手法

ソースからビルド (make install なし)

-D CMAKE_PREFIX_PATH="externals/googletest/v1.13.0/lib/cmake/GTest のようにして、find_packge した結果は下記

1
2
3
4
5
6
-- Found GTest: /home/user/Work/test/externals/googletest/v1.13.0/lib/cmake/GTest/GTestConfig.cmake (found version "1.13.0")  
GTest_FOUND: TRUE
GTest_VERSION: 1.13.0
GTEST_INCLUDE_DIRS:
GTEST_LIBRARIES: GTest::gtest
GTEST_MAIN_LIBRARIES: GTest::gtest_main

apt-get install libgtest-dev

Ubuntu 20.04 での結果。
find_packge した結果は下記。

1
2
3
4
5
6
-- Found GTest: /usr/lib/x86_64-linux-gnu/libgtest.a  
GTest_FOUND: TRUE
GTest_VERSION:
GTEST_INCLUDE_DIRS: /usr/include
GTEST_LIBRARIES: /usr/lib/x86_64-linux-gnu/libgtest.a
GTEST_MAIN_LIBRARIES: /usr/lib/x86_64-linux-gnu/libgtest_main.a

これを実行してしまうと /usr/include/gtest/gtest.h がインストールされるため、他の方法で GTEST_INCLUDE_DIRS が見つからなくても、ビルドが通るようになってしまう。

ソースからビルド (make install あり)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
$ cmake --build . --config Release
$ make -j8
$ sudo make install
[ 25%] Built target gtest
[ 50%] Built target gmock
[ 75%] Built target gmock_main
[100%] Built target gtest_main
Install the project...
-- Install configuration: "Release"
-- Up-to-date: /usr/local/include
-- Installing: /usr/local/include/gmock
-- Installing: /usr/local/include/gmock/gmock-more-matchers.h
-- Installing: /usr/local/include/gmock/internal
-- Installing: /usr/local/include/gmock/internal/gmock-pp.h
-- Installing: /usr/local/include/gmock/internal/custom
-- Installing: /usr/local/include/gmock/internal/custom/gmock-generated-actions.h
-- Installing: /usr/local/include/gmock/internal/custom/gmock-matchers.h
-- Installing: /usr/local/include/gmock/internal/custom/README.md
-- Installing: /usr/local/include/gmock/internal/custom/gmock-port.h
-- Installing: /usr/local/include/gmock/internal/gmock-internal-utils.h
-- Installing: /usr/local/include/gmock/internal/gmock-port.h
-- Installing: /usr/local/include/gmock/gmock-actions.h
-- Installing: /usr/local/include/gmock/gmock-more-actions.h
-- Installing: /usr/local/include/gmock/gmock.h
-- Installing: /usr/local/include/gmock/gmock-cardinalities.h
-- Installing: /usr/local/include/gmock/gmock-function-mocker.h
-- Installing: /usr/local/include/gmock/gmock-matchers.h
-- Installing: /usr/local/include/gmock/gmock-spec-builders.h
-- Installing: /usr/local/include/gmock/gmock-nice-strict.h
-- Installing: /usr/local/lib/libgmock.a
-- Installing: /usr/local/lib/libgmock_main.a
-- Installing: /usr/local/lib/pkgconfig/gmock.pc
-- Installing: /usr/local/lib/pkgconfig/gmock_main.pc
-- Installing: /usr/local/lib/cmake/GTest/GTestTargets.cmake
-- Installing: /usr/local/lib/cmake/GTest/GTestTargets-release.cmake
-- Installing: /usr/local/lib/cmake/GTest/GTestConfigVersion.cmake
-- Installing: /usr/local/lib/cmake/GTest/GTestConfig.cmake
-- Up-to-date: /usr/local/include
-- Installing: /usr/local/include/gtest
-- Installing: /usr/local/include/gtest/gtest-matchers.h
-- Installing: /usr/local/include/gtest/internal
-- Installing: /usr/local/include/gtest/internal/custom
-- Installing: /usr/local/include/gtest/internal/custom/gtest.h
-- Installing: /usr/local/include/gtest/internal/custom/gtest-printers.h
-- Installing: /usr/local/include/gtest/internal/custom/README.md
-- Installing: /usr/local/include/gtest/internal/custom/gtest-port.h
-- Installing: /usr/local/include/gtest/internal/gtest-string.h
-- Installing: /usr/local/include/gtest/internal/gtest-type-util.h
-- Installing: /usr/local/include/gtest/internal/gtest-filepath.h
-- Installing: /usr/local/include/gtest/internal/gtest-port-arch.h
-- Installing: /usr/local/include/gtest/internal/gtest-param-util.h
-- Installing: /usr/local/include/gtest/internal/gtest-internal.h
-- Installing: /usr/local/include/gtest/internal/gtest-death-test-internal.h
-- Installing: /usr/local/include/gtest/internal/gtest-port.h
-- Installing: /usr/local/include/gtest/gtest-death-test.h
-- Installing: /usr/local/include/gtest/gtest-message.h
-- Installing: /usr/local/include/gtest/gtest-test-part.h
-- Installing: /usr/local/include/gtest/gtest.h
-- Installing: /usr/local/include/gtest/gtest-typed-test.h
-- Installing: /usr/local/include/gtest/gtest_pred_impl.h
-- Installing: /usr/local/include/gtest/gtest-printers.h
-- Installing: /usr/local/include/gtest/gtest_prod.h
-- Installing: /usr/local/include/gtest/gtest-spi.h
-- Installing: /usr/local/include/gtest/gtest-param-test.h
-- Installing: /usr/local/include/gtest/gtest-assertion-result.h
-- Installing: /usr/local/lib/libgtest.a
-- Installing: /usr/local/lib/libgtest_main.a
-- Installing: /usr/local/lib/pkgconfig/gtest.pc
-- Installing: /usr/local/lib/pkgconfig/gtest_main.pc

find_packge した結果は下記。

1
2
3
4
5
6
-- Found GTest: /usr/local/lib/cmake/GTest/GTestConfig.cmake (found version "1.13.0")  
GTest_FOUND: TRUE
GTest_VERSION: 1.13.0
GTEST_INCLUDE_DIRS:
GTEST_LIBRARIES: GTest::gtest
GTEST_MAIN_LIBRARIES: GTest::gtest_main

/usr/lib よりも、**/usr/local/lib** が優先される模様。

FetchContent_Declare

公式ページで説明されている手順。

CMakeLists.txt に下記のような記載を加えることで任意のバージョンの googletest を利用できる。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cmake_minimum_required(VERSION 3.14)
project(my_project)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

FetchContent_MakeAvailable 等が CMake 3.14 以降でしか使えないので注意。

最小のサンプルは下記。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cmake_minimum_required(VERSION 3.14)

# GoogleTest requires at least C++14
set(PROJ_NAME test)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(${PROJ_NAME} VERSION 1.0.0.0)

# b796f7d44681514f58a683a3a71ff17c94edb0c1 is v1.13.0
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/b796f7d44681514f58a683a3a71ff17c94edb0c1.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

add_executable(${PROJ_NAME}
${PROJECT_SOURCE_DIR}/main.cpp
)

target_link_libraries(${PROJ_NAME}
gtest
gtest_main
)

target_include_directories でヘッダーを指定しなくても動く。
(綺麗な環境で試験済み)