Fix CMake “Could not find a package configuration file” (find_package Errors)

, ,
Fix CMake Could not find a package configuration file (find_package Errors)

Few CMake messages stop a build as quickly as this one: Could not find a package configuration file provided by “Foo”. The library might even be installed. CMake simply could not find the file that describes it, in the mode it was told to search.

This guide explains how find_package searches on Linux, how to read the error, and the fixes that work, from installing the right -dev package to setting CMAKE_PREFIX_PATH correctly. The commands assume CMake 3.x or newer; option names are stable across recent releases.

Quick fix: Install the library’s development package (for example libfmt-dev), or point CMake at the install prefix: cmake -S . -B build -DCMAKE_PREFIX_PATH=/opt/foo. Then delete build/CMakeCache.txt and configure again.

What does the error actually say?

A typical failure looks like this:

CMake Error at CMakeLists.txt:8 (find_package):
  By not providing "Findfmt.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "fmt", but
  CMake did not find one.

  Could not find a package configuration file provided by "fmt" with any of
  the following names:

    fmtConfig.cmake
    fmt-config.cmake

  Add the installation prefix of "fmt" to CMAKE_PREFIX_PATH or set "fmt_DIR"
  to a directory containing one of the above files.

Three facts are hidden in that text. CMake found no Findfmt.cmake module. It then searched for fmtConfig.cmake or fmt-config.cmake. And it is telling you the two variables that control the search.

How does find_package search? Module mode vs Config mode

Module mode looks for a Find<Package>.cmake script in CMAKE_MODULE_PATH and CMake’s own modules. CMake ships many of these, such as FindThreads, FindZLIB, and FindOpenSSL.

Config mode looks for a <Package>Config.cmake file installed by the library itself, usually under <prefix>/lib/cmake/<Package>/ or <prefix>/share/<Package>/cmake/. Modern libraries such as fmt, spdlog, gRPC, and Protobuf provide these.

By default CMake tries module mode first, then config mode. The error above appears when both fail.

Where CMake looks (simplified order) How you influence it
<Package>_DIR Set it to the directory containing the config file
<Package>_ROOT (CMake 3.12+) Set to the package’s install prefix
CMAKE_PREFIX_PATH Cache variable or environment variable listing prefixes
PATH-derived prefixes e.g. /opt/foo/bin implies prefix /opt/foo
System prefixes /usr/local, /usr, and multiarch dirs such as lib/x86_64-linux-gnu/cmake
Flowchart of how CMake find_package searches for FooConfig.cmake: Foo_DIR, then CMAKE_PREFIX_PATH or Foo_ROOT, then system prefixes, before failing with Could not find a package configuration file.
How find_package looks for a FooConfig.cmake file, and where the error comes from.

Fix 1: Is the development package installed?

On Debian and Ubuntu, the runtime library and its headers plus CMake files are often split. libfmt9 gives you the .so; libfmt-dev gives you headers and fmtConfig.cmake.

  1. Install the dev package:
    # Debian/Ubuntu
    sudo apt install libfmt-dev
    # Fedora
    sudo dnf install fmt-devel
    # Arch
    sudo pacman -S fmt

    You should see the package manager report that the package was installed or is already the newest version.

  2. Confirm the config file exists:
    find /usr /usr/local -name 'fmt*Config.cmake' 2>/dev/null

    You should see a path such as /usr/lib/x86_64-linux-gnu/cmake/fmt/fmt-config.cmake.

Tip: On Debian-based systems, apt-file search fmt-config.cmake tells you which package ships a given file. Install apt-file and run sudo apt-file update first.

Fix 2: Point CMake at a custom install prefix

If you built the library yourself with cmake --install build --prefix /opt/fmt, CMake will not look in /opt/fmt unless told to.

  1. Pass the prefix on the command line:
    rm -rf build
    cmake -S . -B build -DCMAKE_PREFIX_PATH=/opt/fmt

    You should see configuration finish with -- Configuring done and -- Generating done.

  2. Or set the exact directory:
    cmake -S . -B build -Dfmt_DIR=/opt/fmt/lib/cmake/fmt

    You should see the same success lines. fmt_DIR must point at the folder containing the config file, not at the prefix.

Multiple prefixes are separated by semicolons on the command line: -DCMAKE_PREFIX_PATH="/opt/fmt;/opt/spdlog". As an environment variable, use colons on Linux.

Warning: CMake caches results. After a failed search, fmt_DIR is stored as fmt_DIR-NOTFOUND in CMakeCache.txt. Fixing the path without deleting the cache (or the build directory) often seems to change nothing.

Fix 3: Check the package name and case

The name passed to find_package must match the config file name. Common mismatches include find_package(OpenCV) (correct) vs opencv, find_package(Protobuf) for the module vs protobuf for the config file, and find_package(Qt6 COMPONENTS Widgets) where the component is what fails.

ls /usr/lib/x86_64-linux-gnu/cmake/ | grep -i proto

The directory name usually tells you the exact spelling CMake expects.

Fix 4: Use the right mode, or fall back to pkg-config

Some libraries ship only a pkg-config .pc file and no CMake config. In that case use CMake’s PkgConfig module:

find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBSODIUM REQUIRED IMPORTED_TARGET libsodium)
target_link_libraries(app PRIVATE PkgConfig::LIBSODIUM)

Typical success output contains -- Checking for module 'libsodium' followed by -- Found libsodium, version .... If you need to force config mode and skip a broken find module, use find_package(Foo CONFIG REQUIRED).

Fix 5: Debug the search with –debug-find

When you cannot tell where CMake is looking, ask it. CMake 3.17 and newer support --debug-find, and newer versions add --debug-find-pkg=<name> to narrow the output.

cmake -S . -B build --debug-find-pkg=fmt 2>&1 | less

The output lists every candidate directory and file name CMake tried. Compare that list with where the config file really lives; the gap is usually obvious.

Fix 6: Cross-compiling, Conda, and vcpkg

When cross-compiling, CMAKE_FIND_ROOT_PATH and CMAKE_FIND_ROOT_PATH_MODE_PACKAGE can restrict searches to a sysroot. A host library will then be invisible on purpose. Inside a Conda environment, add $CONDA_PREFIX to CMAKE_PREFIX_PATH. With vcpkg, pass -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake so vcpkg adds its installed tree.

What does a working CMakeLists.txt look like?

Once the package is found, link against the imported target it defines rather than raw variables. Imported targets carry include paths and compile definitions with them.

cmake_minimum_required(VERSION 3.16)
project(demo LANGUAGES CXX)

find_package(fmt CONFIG REQUIRED)

add_executable(app main.cpp)
target_link_libraries(app PRIVATE fmt::fmt)

Configure and build. Config-mode packages usually do not print a Found line; configuration simply completes and cmake --build build ends with [100%] Built target app. To confirm which config file was used, run grep fmt_DIR build/CMakeCache.txt.

Symptom-cause-fix summary

Symptom Cause Fix
No Config.cmake anywhere on disk Dev package missing Install -dev / -devel package
File exists under /opt or $HOME Prefix not searched CMAKE_PREFIX_PATH or Foo_DIR
Fix applied but error unchanged Stale cache Delete build/ or CMakeCache.txt
Only a .pc file exists No CMake package config shipped Use pkg_check_modules
Package found, but link fails later Wrong target name or missing link See the undefined-reference guide below

FAQ

What is the difference between Foo_DIR and Foo_ROOT?

Foo_DIR points at the directory containing the config file. Foo_ROOT points at the package’s install prefix and is searched like an entry in CMAKE_PREFIX_PATH.

Should I write my own FindFoo.cmake?

Only as a last resort for libraries that ship neither a CMake config nor a pkg-config file. Prefer upstream config files when they exist.

Why does it work in my terminal but fail in the IDE?

IDEs often use a different build directory and environment. An exported CMAKE_PREFIX_PATH in your shell profile may not reach the IDE; set it in the IDE’s CMake settings or a CMakePresets.json file.

Does Bazel have the same problem?

Bazel declares external dependencies explicitly, so this specific error does not exist there. The tradeoffs are covered in our Bazel vs CMake guide.

Conclusion

Read the error literally: which file names, which mode, which variables. Then confirm the file exists, point CMake at its prefix, and clear the cache before re-running. Those three steps resolve the large majority of find_package failures on Linux.

Related guides

Official references

Leave a Reply