# How to mark GMock as a required component

**URL:** https://discourse.cmake.org/t/how-to-mark-gmock-as-a-required-component/10106
**Category:** Usage
**Tags:** os:linux
**Created:** [February 15, 2024, 4:28pm UTC](https://discourse.cmake.org/t/how-to-mark-gmock-as-a-required-component/10106 "2024-02-15T16:28:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ryanf55](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ryanf55/32/3516_2.png) [@Ryanf55](https://discourse.cmake.org/u/Ryanf55)
#### Post date: [February 15, 2024, 4:28pm UTC](https://discourse.cmake.org/t/how-to-mark-gmock-as-a-required-component/10106/1 "2024-02-15T16:28:03Z")

</div>

I’m using CMake 3.27 and use both GTest and GMock in a package.

There are a few options I’ve seen to pull this in:

1. With the FindGTest module

```cmake
include(FindGTest)

```

1. Directly with find\_package

```cmake
find_package(GTest CONFIG REQUIRED COMPONENTS GTest GMock)

```

1. With FetchContent as recommended by GTest, which doesn’t allow us to use the system package version

On Ubuntu 22, `libgtest-dev` and `libgmock-dev` are distributed as separate packages. It’s common to have `libgtest-dev` and not `libgmock-dev`. In that situation,

- `GTest::gtest` will exist
- `GTest::gmock` will not exist

The `libgmock-dev` package comes with `/usr/lib/x86_64-linux-gnu/cmake/GTest/GMockTargets.cmake`

My usage:

```auto
add_executable(MyTest
    main.cpp
)
set(CMAKE_FIND_DEBUG_MODE ON)
find_package(GTest CONFIG REQUIRED COMPONENTS GTest GMock)
set(CMAKE_FIND_DEBUG_MODE OFF)

target_link_libraries(MyTest
    PRIVATE
    ${PROJECT_NAME}::MyLibrary
    GTest::gtest
    GTest::gmock
)

```

Logs when building tests:

```auto
CMake Debug Log at testbench/CMakeLists.txt:10 (find_package):
    /usr/lib/x86_64-linux-gnu/cmake/GTest/GTestConfig.cmake

-- Configuring done (0.3s)
CMake Error at testbench/CMakeLists.txt:13 (target_link_libraries):
  Target "MyTest" links to:

    GTest::gmock

  but the target was not found. Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.

```

Why is there not an error in find\_package when I specify `GMock` as a required component? I would expect an error that the component was not found and a find\_package result of failure.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [February 15, 2024, 5:00pm UTC](https://discourse.cmake.org/t/how-to-mark-gmock-as-a-required-component/10106/2 "2024-02-15T17:00:23Z")

</div>

### It works fine:

```auto
$ cmake -B build -S . -G Ninja --debug-find-pkg=GTest
Running with debug output on for the 'find' commands for package(s) GTest.
CMake Debug Log at /home/klein_cl/.local/lib/python3.10/site-packages/cmake/data/share/cmake-3.28/Modules/CMakeFindDependencyMacro.cmake:76 (find_package):
  find_package considered the following paths for FindThreads.cmake:

  The file was found at

    /home/klein_cl/.local/lib/python3.10/site-packages/cmake/data/share/cmake-3.28/Modules/FindThreads.cmake

Call Stack (most recent call first):
  /usr/lib/x86_64-linux-gnu/cmake/GTest/GTestConfig.cmake:39 (find_dependency)
  CMakeLists.txt:6 (find_package)

CMake Debug Log at CMakeLists.txt:6 (find_package):
    /usr/lib/x86_64-linux-gnu/cmake/GTest/GTestConfig.cmake

-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/klein_cl/Workspace/cpp/test/build

$ tree /usr/lib/x86_64-linux-gnu/cmake/GTest/
/usr/lib/x86_64-linux-gnu/cmake/GTest/
├── GMockTargets-none.cmake
├── GMockTargets.cmake
├── GTestConfig.cmake
├── GTestConfigVersion.cmake
├── GTestTargets-none.cmake
└── GTestTargets.cmake

0 directories, 6 files

```

---

<div class="post-metadata">

### Author: ![Ryanf55](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ryanf55/32/3516_2.png) [@Ryanf55](https://discourse.cmake.org/u/Ryanf55)
#### Post date: [February 17, 2024, 5:06pm UTC](https://discourse.cmake.org/t/how-to-mark-gmock-as-a-required-component/10106/3 "2024-02-17T17:06:09Z")

</div>

> [@ClausKlein](#):
>
> `-debug-find-pkg=GTes`

Uninstall GMock, and try again? That’s when it exhibited poor behavior for me.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [February 17, 2024, 6:59pm UTC](https://discourse.cmake.org/t/how-to-mark-gmock-as-a-required-component/10106/4 "2024-02-17T18:59:57Z")

</div>

I guess it is a problem with [find\_dependency()](https://cmake.org/cmake/help/latest/module/CMakeFindDependencyMacro.html#cmakefinddependencymacro)

Replace in `/usr/lib/x86_64-linux-gnu/cmake/GTest/GTestConfig.cmake` `find_dependency(x)` with `find_package(x REQUIRED)` and try again.

---

<div class="post-metadata">

### Author: ![Ryanf55](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ryanf55/32/3516_2.png) [@Ryanf55](https://discourse.cmake.org/u/Ryanf55)
#### Post date: [February 28, 2024, 4:22am UTC](https://discourse.cmake.org/t/how-to-mark-gmock-as-a-required-component/10106/5 "2024-02-28T04:22:34Z")

</div>

> [@ClausKlein](#):
>
> /usr/lib/x86\_64-linux-gnu/cmake/GTest/GTestConfig.cmake

Also, it seems odd at the end of that file, it says that the `GMockTargets.cmake` file is optional

```auto
include("${CMAKE_CURRENT_LIST_DIR}/GMockTargets.cmake" OPTIONAL)

```

Shouldn’t the optional keyword be added if `<PackageName>_FIND_COMPONENTS` ie, `GTest_FIND_COMPONENTS` is missing GMock?  
Reference:  
[https://cmake.org/cmake/help/latest/command/find\_package.html#package-file-interface-variables](https://cmake.org/cmake/help/latest/command/find_package.html#package-file-interface-variables)

---

<div class="post-metadata">

### Author: ![marlord](https://discourse.cmake.org/user_avatar/discourse.cmake.org/marlord/32/5625_2.png) [@marlord](https://discourse.cmake.org/u/marlord)
#### Post date: [June 19, 2025, 8:38pm UTC](https://discourse.cmake.org/t/how-to-mark-gmock-as-a-required-component/10106/6 "2025-06-19T20:38:59Z")

</div>

Yes it seems like there is a problem with finding the GMock. I tried several combinations and the finding does not create either the GMock Target or the {} variable. At the end, the workaround is to just add the “gmock” as the linking dependency and it will grab it with a flag…
