Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

CMake cannot find GoogleTest (find_package(GTest) fails) on Ubuntu

ErrorCould NOT find GTest

Short answer

find_package(GTest) needs an installed GoogleTest. On Ubuntu install libgtest-dev, delete the CMake cache, and link GTest::gtest or GTest::gtest_main. If GoogleTest lives in a custom prefix, give CMake that prefix. A package built with CMake is found in config mode first.

  • Applies to CMake 3.28, GoogleTest 1.14, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether the package is installed, then configure from a clean build folder so a cached failure is not reused.

    dpkg -l libgtest-dev
    rm -rf build && cmake -S . -B build
  2. 2Match what you see, then jump to its fix

    1. 1dpkg reports no such package, and find_package(GTest REQUIRED) stops configurationGoogleTest is not installedFix
    2. 2You built GoogleTest yourself and installed it under /opt or your home folderGoogleTest is installed in a prefix CMake does not searchFix
    3. 3GTest is found, but the build fails at link time with undefined references to testing:: symbols or mainThe test target links the wrong nameFix

What does the error look like?

  • Could NOT find GTest
  • find_package(GTest REQUIRED) stops configuration

Cause 1 of 3

GoogleTest is not installed

1Check

libgtest-dev is in the universe section of Ubuntu 24.04 (version 1.14.0-1).

apt-cache policy libgtest-dev

2Fix

Install it, then configure again.

sudo apt install libgtest-dev

3Verify

Configure from scratch.

rm -rf build
cmake -S . -B build

Confirmed by packages.ubuntu.com

Did this fix it?

Cause 2 of 3

GoogleTest is installed in a prefix CMake does not search

1Check

Since CMake 3.20 the module first looks for GTestConfig.cmake from a CMake-built install, then falls back to standard locations. GTEST_ROOT is only a hint for that fallback.

find /opt "$HOME" -name 'GTestConfig.cmake' 2>/dev/null

2Fix

Add the install prefix to CMAKE_PREFIX_PATH, or set GTEST_ROOT for the module-mode lookup.

cmake -S . -B build -DCMAKE_PREFIX_PATH=/opt/googletest
# or
cmake -S . -B build -DGTEST_ROOT=/opt/googletest

3Verify

The configure step prints that GTest was found.

cmake -S . -B build | grep -i gtest

Confirmed by cmake.org

Did this fix it?

Cause 3 of 3

The test target links the wrong name

1Check

GTest::gtest is the core framework. GTest::gtest_main also supplies main(), so link it only when your tests do not define their own.

grep -n 'gtest' CMakeLists.txt

2Fix

Link the imported target that matches your test file.

find_package(GTest REQUIRED)
add_executable(unit_tests tests.cpp)
target_link_libraries(unit_tests PRIVATE GTest::gtest_main)

3Verify

Build and run the tests.

cmake --build build
ctest --test-dir build

Confirmed by cmake.org

Did this fix it?

How do I stop it coming back?

  • List libgtest-dev in the build requirements, or fetch GoogleTest with FetchContent for a self-contained build.
  • Link imported targets, not paths to .a files.
  • Delete the build folder after installing a missing package.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.