Package Config Files
This lesson finishes the install story. A consumer will write
find_package(Greet 1.2 REQUIRED)
target_link_libraries(app PRIVATE Greet::greet)and for that to work, the installed package needs two files in lib/cmake/Greet/:
| File | Purpose | Generated with |
|---|---|---|
GreetConfig.cmake |
the entry point find_package loads: finds dependencies, includes the targets file |
configure_package_config_file() from a small template |
GreetConfigVersion.cmake |
answers "is this version compatible with what was requested?" | write_basic_package_version_file() |
Both helpers come from the CMakePackageConfigHelpers module.
The config template
# cmake/GreetConfig.cmake.in
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
find_dependency(Threads) # every find_package your PUBLIC interface needs
include("${CMAKE_CURRENT_LIST_DIR}/GreetTargets.cmake")
check_required_components(Greet)@PACKAGE_INIT@expands to helper code that computes the install prefix relative to the file, so the package stays relocatable.find_dependency()isfind_package()for config files: it forwardsQUIET/REQUIREDand reports a missing dependency properly. IfGreet::greetlinksThreads::ThreadsPUBLIC, the consumer needsThreads::Threadsto exist, so the config file must find it.check_required_components()fails if a consumer asked for a component the package doesn't provide.
The full library, installed and consumed
This session builds and installs Greet 1.4.0 (with a real dependency on Threads), then a second project finds it with find_package, builds and runs. Finally it asks for version 2.0 to see the version check fail.
mkdir -p greet/include/greet greet/src greet/cmake && cd greet
cat > include/greet/greet.hpp <<'SRC'
#pragma once
#include <string>
namespace greet { std::string hello(const std::string& who); }
SRC
cat > src/greet.cpp <<'SRC'
#include <greet/greet.hpp>
#include <future>
std::string greet::hello(const std::string& who) {
return std::async(std::launch::async, [&] { return "Hello, " + who + "!"; }).get();
}
SRC
cat > cmake/GreetConfig.cmake.in <<'CM'
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
find_dependency(Threads)
include("${CMAKE_CURRENT_LIST_DIR}/GreetTargets.cmake")
check_required_components(Greet)
CM
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Greet VERSION 1.4.0 LANGUAGES CXX)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
find_package(Threads REQUIRED)
add_library(greet src/greet.cpp)
add_library(Greet::greet ALIAS greet)
target_include_directories(greet PUBLIC
lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
lt;INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_compile_features(greet PUBLIC cxx_std_17)
target_link_libraries(greet PUBLIC Threads::Threads)
set(cmake_dir ${CMAKE_INSTALL_LIBDIR}/cmake/Greet)
install(TARGETS greet EXPORT GreetTargets)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT GreetTargets NAMESPACE Greet:: DESTINATION ${cmake_dir})
configure_package_config_file(cmake/GreetConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/GreetConfig.cmake
INSTALL_DESTINATION ${cmake_dir})
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/GreetConfigVersion.cmake
COMPATIBILITY SameMajorVersion)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/GreetConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/GreetConfigVersion.cmake
DESTINATION ${cmake_dir})
CM
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_BUILD_TYPE=Release > /dev/null
cmake --build build > /dev/null
cmake --install build --prefix "$HOME/sdk" > /dev/null
ls ~/sdk/lib/cmake/Greet
# --- a separate project that uses the installed package
mkdir -p ~/consumer && cd ~/consumer
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Consumer LANGUAGES CXX)
find_package(Greet 1.2 REQUIRED)
message("Found Greet ${Greet_VERSION} in ${Greet_DIR}")
add_executable(app main.cpp)
target_link_libraries(app PRIVATE Greet::greet)
CM
cat > main.cpp <<'SRC'
#include <iostream>
#include <greet/greet.hpp>
int main() { std::cout << greet::hello("installed package") << '\n'; }
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_PREFIX_PATH="$HOME/sdk"
cmake --build build && ./build/app
echo "--- ask for an incompatible version"
sed -i 's/Greet 1.2/Greet 2.0/' CMakeLists.txt
cmake -S . -B build-v2 -G Ninja --log-level=NOTICE -DCMAKE_PREFIX_PATH="$HOME/sdk"GreetConfig.cmake
GreetConfigVersion.cmake
GreetTargets-release.cmake
GreetTargets.cmake
Found Greet 1.4.0 in /home/learner/sdk/lib/cmake/Greet
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/consumer/build
[1/2] Building CXX object CMakeFiles/app.dir/main.cpp.o
[2/2] Linking CXX executable app
Hello, installed package!
--- ask for an incompatible version
CMake Error at CMakeLists.txt:3 (find_package):
Could not find a configuration file for package "Greet" that is compatible
with requested version "2.0".
The following configuration files were considered but not accepted:
/home/learner/sdk/lib/cmake/Greet/GreetConfig.cmake, version: 1.4.0
The version found is not compatible with the version requested.
-- Configuring incomplete, errors occurred!
The consumer's CMakeLists.txt mentions neither an include path nor cxx_std_17 nor Threads: all of that came through Greet::greet. The consumer was found through CMAKE_PREFIX_PATH=$HOME/sdk, the prefix, and find_package searched lib/cmake/Greet inside it.
Version compatibility
write_basic_package_version_file takes a COMPATIBILITY policy:
| COMPATIBILITY | Package 1.4.0 accepts a request for | Rejects |
|---|---|---|
AnyNewerVersion |
1.0, 1.4, 0.9 | 1.5, 2.0 |
SameMajorVersion |
1.0, 1.4 | 0.9, 1.5, 2.0 |
SameMinorVersion |
1.4, 1.4.0 | 1.2, 1.5, 2.0 |
ExactVersion |
1.4.0 only | anything else, even 1.4 |
This session checks the table with the real find_package: it writes a version file for package G 1.4.0 in each mode (the config file itself is empty), then requests six versions against each:
for mode in AnyNewerVersion SameMajorVersion SameMinorVersion ExactVersion; do
mkdir -p pk$mode/lib/cmake/G
cat > pk$mode/lib/cmake/G/GConfig.cmake <<'CM'
CM
cat > mk.cmake <<CM
include(CMakePackageConfigHelpers)
write_basic_package_version_file($PWD/pk$mode/lib/cmake/G/GConfigVersion.cmake VERSION 1.4.0 COMPATIBILITY $mode)
CM
cmake -P mk.cmake
done
mkdir c && cd c
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(C LANGUAGES NONE)
foreach(mode AnyNewerVersion SameMajorVersion SameMinorVersion ExactVersion)
set(row "")
foreach(r 0.9 1.2 1.4 1.4.0 1.5 2.0)
unset(G_DIR CACHE)
find_package(G ${r} QUIET NO_DEFAULT_PATH PATHS $ENV{HOME}/pk${mode})
string(APPEND row " ${r}:${G_FOUND}")
endforeach()
message("${mode}${row}")
endforeach()
CM
cmake -S . -B b --log-level=NOTICE 2>&1 | grep -v '^--'AnyNewerVersion 0.9:1 1.2:1 1.4:1 1.4.0:1 1.5:0 2.0:0
SameMajorVersion 0.9:0 1.2:1 1.4:1 1.4.0:1 1.5:0 2.0:0
SameMinorVersion 0.9:0 1.2:0 1.4:1 1.4.0:1 1.5:0 2.0:0
ExactVersion 0.9:0 1.2:0 1.4:0 1.4.0:1 1.5:0 2.0:0
1 means found. Note that ExactVersion rejects a request for 1.4: it wants all three components to match.
For a library that follows semantic versioning, SameMajorVersion is the right choice. Add ARCH_INDEPENDENT for header-only packages, so a package installed by a 64-bit build can be used by a 32-bit one.
Where find_package looks
For each prefix in CMAKE_PREFIX_PATH (and the system prefixes such as /usr/local and /usr), Config mode tries a list of subdirectories, including <prefix>/lib/cmake/<name>*/, <prefix>/lib/<arch>/cmake/<name>*/, <prefix>/share/<name>*/cmake/ and <prefix>/<name>*/. Installing to ${CMAKE_INSTALL_LIBDIR}/cmake/<Name> (or ${CMAKE_INSTALL_DATADIR}/<name>/cmake for header-only packages) is the convention.
Note: CMake 4 also understands the Common Package Specification (
<Name>.cps, a JSON format shared with other build systems), which is why the "not found" message listsGreet.cps. Exporting CPS files withinstall(PACKAGE_INFO …)is new;<Name>Config.cmakeremains the format every CMake version understands.
Test yourself
Which file decides whether an installed package version satisfies find_package(Greet 1.2)?
- GreetConfig.cmake
- GreetConfigVersion.cmake
- GreetTargets.cmake
- CMakeCache.txt
Greet::greet links Threads::Threads PUBLIC. What must GreetConfig.cmake do?
- Nothing
- Call find_dependency(Threads) before including the targets file
- Copy libpthread into the package
- Export Threads::Threads
Package version 1.4.0 with SameMajorVersion. Which request is accepted?
- 2.0
- 1.5
- 1.2
- 0.9