find_package: Module vs Config Mode
Most projects use libraries they don't build themselves: system libraries, SDKs, packages installed by a package manager. find_package() locates such a library and, in modern CMake, gives you an imported target like ZLIB::ZLIB that you link exactly like your own targets.
find_package(<Name> [<version>] [REQUIRED] [COMPONENTS <c>...] [CONFIG|MODULE])
target_link_libraries(app PRIVATE <Name>::<target>)Two ways to find a package
find_package(Foo) can work in two modes:
| Module mode | Config mode | |
|---|---|---|
| Looks for | FindFoo.cmake in CMAKE_MODULE_PATH, then CMake's own modules |
FooConfig.cmake or foo-config.cmake (and, since CMake 4, Foo.cps) in package prefixes |
| Written by | CMake developers or you (it searches for headers and libraries) | the package itself, generated when the package was installed |
| Typical for | system libraries that don't ship CMake files (ZLIB, Threads, OpenSSL, CURL) | packages built with CMake (fmt, spdlog, Qt 6, your own libraries) |
| Accuracy | guesses from file names | exact: the package describes its targets, dependencies and version |
By default CMake tries Module mode first and falls back to Config mode. find_package(Foo CONFIG) forces Config mode, MODULE forces Module mode.
Trying both in a real session
The image these lessons run in has no zlib development files, but it does have threads support and libxcrypt with a pkg-config file. This session tries a few packages:
mkdir -p finds && cd finds
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Finds LANGUAGES C)
find_package(Threads REQUIRED) # Module mode: CMake's FindThreads.cmake
message("Threads_FOUND = ${Threads_FOUND}")
find_package(ZLIB) # Module mode: FindZLIB.cmake, not installed here
message("ZLIB_FOUND = ${ZLIB_FOUND}")
find_package(PkgConfig REQUIRED) # pkg-config wrapper module
pkg_check_modules(XCRYPT REQUIRED IMPORTED_TARGET libxcrypt)
find_package(fmt CONFIG) # Config mode only: looks for fmtConfig.cmake
message("fmt_FOUND = ${fmt_FOUND}")
add_executable(app main.c)
target_link_libraries(app PRIVATE Threads::Threads PkgConfig::XCRYPT)
CM
cat > main.c <<'SRC'
#include <crypt.h>
#include <stdio.h>
#include <string.h>
int main(void) {
const char *h = crypt("secret", "$5$saltsaltquot;);
printf("crypt prefix: %.11s (length %zu)\n", h, strlen(h));
return 0;
}
SRC
cmake -S . -B build -G Ninja 2>&1 | grep -v -e 'compiler' -e 'C compile' -e 'ABI info'
cmake --build build > /dev/null && ./build/app-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
Threads_FOUND = TRUE
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
ZLIB_FOUND = FALSE
-- Found PkgConfig: /usr/bin/pkg-config (found version "1.8.1")
-- Checking for module 'libxcrypt'
-- Found libxcrypt, version 4.4.36
-- Could NOT find fmt (missing: fmt_DIR)
fmt_FOUND = 0
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/finds/build
crypt prefix: $5$saltsalt (length 55)
Things to read in that output:
Found Threads: TRUEandCould NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)are printed byfind_package_handle_standard_args, which every good Find module uses. Themissing:list names the cache variables it couldn't fill, so you know what to set if the library is somewhere unusual (-DZLIB_ROOT=/opt/zlib).pkg_check_modules(... IMPORTED_TARGET ...)turned the.pcfile into a target,PkgConfig::XCRYPT. That's the bridge for libraries that ship pkg-config files but no CMake config.fmtwas not found and, withoutREQUIRED, that's only a status line (andfmt_FOUNDis a false value, here0). WithREQUIREDthe configure stops.
What a failed Config-mode search says
When neither a Find module nor a config file exists, CMake explains exactly where it looked:
mkdir -p missing && cd missing
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Missing LANGUAGES NONE)
find_package(Greet 1.2 REQUIRED)
CM
cmake -S . -B build --log-level=NOTICECMake Error at CMakeLists.txt:3 (find_package):
By not providing "FindGreet.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Greet", but
CMake did not find one.
Could not find a package configuration file provided by "Greet" (requested
version 1.2) with any of the following names:
Greet.cps
greet.cps
GreetConfig.cmake
greet-config.cmake
Add the installation prefix of "Greet" to CMAKE_PREFIX_PATH or set
"Greet_DIR" to a directory containing one of the above files. If "Greet"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
The fixes it suggests are the two ways to point CMake at a package:
CMAKE_PREFIX_PATH: a list of installation prefixes (/opt/greet,$HOME/sdk). CMake searches<prefix>/lib/cmake/Greet/,<prefix>/share/greet/and several other standard places under each prefix. This is what package managers set for you.Greet_DIR: the exact directory containingGreetConfig.cmake.Greet_ROOT(or theGREET_ROOTenvironment variable) is a third option: a prefix for this one package only.
The Package Config Files lesson creates GreetConfig.cmake and makes this exact command succeed.
Versions and components
find_package(Boost 1.83 REQUIRED COMPONENTS filesystem json)
find_package(Qt6 REQUIRED COMPONENTS Widgets OPTIONAL_COMPONENTS Svg)- The version is checked by the package's version file (Config mode) or by the Find module.
find_package(Greet 1.2)accepts 1.4 if the package declares compatibility;1.2...<2.0is a version range. - Components select parts of a big package. A missing
REQUIREDcomponent fails the configure; a missingOPTIONAL_COMPONENTSone just setsQt6_Svg_FOUNDto false.
Results to rely on
After find_package(Foo), use Foo_FOUND and the imported targets the package documents. The old-style variables (FOO_INCLUDE_DIRS, FOO_LIBRARIES) still exist in many modules, but targets carry everything (include paths, definitions, transitive dependencies, per-configuration library paths) in one name.
Tip:
cmake --debug-find-pkg=Greet -S . -B buildprints every path CMake tried for one package; the Debugging toolkit lesson shows it.
Test yourself
find_package(fmt) with no mode keyword. What does CMake try?
- Only fmtConfig.cmake
- Only Findfmt.cmake
- Findfmt.cmake first (Module mode), then fmtConfig.cmake (Config mode)
- pkg-config
Who writes a package's FooConfig.cmake?
- CMake developers
- The package itself, generated by its install rules
- The user, by hand, always
- pkg-config
A package is installed under /opt/greet (with /opt/greet/lib/cmake/Greet/GreetConfig.cmake). Which setting lets find_package find it?
-DCMAKE_PREFIX_PATH=/opt/greet-DCMAKE_MODULE_PATH=/opt/greet-DCMAKE_INSTALL_PREFIX=/opt/greet-DGreet_FOUND=ON