In CMake, a consumer's find_package(geom) fails with "The link interface of target geom::geom contains: Threads::Threads but the target was not found"; what is missing?
answer
- the export names, but does not define
- imported targets from other packages
- CMakeFindDependencyMacro
- before including the targets file
basics
~10 sgeomConfig.cmake never re-finds the packages geom's exported link interface depends on. install(EXPORT) records the name Threads::Threads but does not define it, so the config file must call find_dependency(Threads) before including geomTargets.cmake.
solid answer
~40 s`geom` links `Threads::Threads` `PUBLIC`, so the generated `geomTargets.cmake` lists `Threads::Threads` in `geom::geom`'s `INTERFACE_LINK_LIBRARIES`. That is only a name: the imported target comes from `find_package(Threads)`, which ran in `geom`'s own build but never runs in the consumer's. Because the name contains `::`, CMake knows it must be a target and stops with "The link interface of target "geom::geom" contains: Threads::Threads but the target was not found", raised from inside `geomTargets.cmake`. The fix belongs in the package: `include(CMakeFindDependencyMacro)` and `find_dependency(Threads)` in `geomConfig.cmake`, before the targets file is included. `find_dependency` forwards `QUIET` and `REQUIRED` from the consumer's call and, if the dependency is missing, ends the config file with a diagnostic instead of a confusing error.
code
cmake · 7 lines@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
find_dependency(Threads) # geom links Threads::Threads PUBLIC
include("${CMAKE_CURRENT_LIST_DIR}/geomTargets.cmake")
check_required_components(geom)go deeper
Recall that a package's config file must find the other packages its library depends on.
Explain why an export names imported targets without defining them, and what find_dependency forwards and does on failure.
Diagnose the target-not-found error as an incomplete package, decide which dependencies need find_dependency from the link interface, and test packages with a minimal consumer in CI.
Own the package's dependency contract: every public dependency a library adds becomes a dependency every consuming team must be able to satisfy.
## The symptom Another team installs `geom`, adds `find_package(geom REQUIRED)` and `target_link_libraries(app PRIVATE geom::geom)`, and configuring fails inside the package itself. In a CMake 4.4.4 run: ```plaintext CMake Error at <prefix>/lib/cmake/geom/geomTargets.cmake:61 (set_target_properties): The link interface of target "geom::geom" contains: Threads::Threads but the target was not found. Possible reasons include: ... * A find_package call is missing for an IMPORTED target. ``` Nothing is wrong with the consumer's code. The package is incomplete. ## Why the export cannot carry the dependency `geom`'s build did this: ```cmake find_package(Threads REQUIRED) target_link_libraries(geom PUBLIC Threads::Threads PRIVATE units) ``` `Threads::Threads` is an **imported target**: created by `find_package(Threads)` in that configure run, visible only in that build. `install(EXPORT)` writes CMake code that recreates the targets in the export set, `geom::geom` and `geom::units`, but only *refers* to targets from other packages by name. The generated file shows it directly: ```cmake INTERFACE_LINK_LIBRARIES "Threads::Threads;\$<LINK_ONLY:geom::units>" ``` When a consumer loads that file, `Threads::Threads` exists only if something in the consumer's configure already called `find_package(Threads)`. The package must make sure of that itself. ## The fix: find_dependency in the config file The `CMakeFindDependencyMacro` module provides `find_dependency()`, a wrapper around `find_package()` designed for package configuration files: ```cmake @PACKAGE_INIT@ include(CMakeFindDependencyMacro) find_dependency(Threads) include("${CMAKE_CURRENT_LIST_DIR}/geomTargets.cmake") check_required_components(geom) ``` What it does, as the module documents: - forwards the `QUIET` and `REQUIRED` options the consumer passed to `find_package(geom ...)`, so a quiet search stays quiet; - forwards any extra arguments, such as a version or `COMPONENTS`, to `find_package`; - if the dependency is not found, sets an informative message and calls `return()`, ending the config file so `geom` is reported as not found, with the reason. With that template, the same consumer configured, built and ran. ## Why the error is raised inside the package The failure is reported from the `set_target_properties` call in `geomTargets.cmake` that sets `geom::geom`'s link interface, so it surfaces while the package loads rather than in the consumer's own code. Names containing `::` are always treated as targets (policy `CMP0028`, whose old behaviour CMake 4.0 removed), so `Threads::Threads` cannot fall back to being treated as a file on disk. The policy's documentation explains why that fallback was dropped: a typo or missing target used to be taken for a file name and led to confusing errors later. The "Possible reasons" list CMake prints names the real one here: a `find_package` call is missing for an `IMPORTED` target. ## Which dependencies need it A practical rule is to mirror the link interface of every exported target: 1. **`PUBLIC` and `INTERFACE` dependencies that are imported targets** from other packages: always. 2. **`PRIVATE` dependencies of a `STATIC` library** that are imported targets: also, because a static library's private link dependencies remain in its link interface as `$<LINK_ONLY:...>` entries, and consumers' linkers need them. 3. **`PRIVATE` dependencies of a `SHARED` library**: normally not, because the shared library already resolved them when it was linked; the loader finds them at run time. 4. **Targets of the same project**, like `units`: not through `find_dependency`; they must be in the export set instead. Calling `find_dependency` *before* including the targets file matters: the check that fails happens while the targets file sets properties. ## Keeping the two lists in sync The link interface lives in `CMakeLists.txt` and the `find_dependency` calls in a template, so they drift. Habits that help: - review the template whenever a `target_link_libraries` line of an exported target changes; - forward the same version constraint the build required, such as `find_dependency(ZLIB 1.2)`; - in CI, install the package into a fresh prefix and configure a minimal consumer that does nothing but `find_package(geom REQUIRED)` and link `geom::geom`, which catches a missing call immediately; - for an optional dependency, guard the `find_dependency` with the same option the build used and record that option's value in the config file.
- Why does the consumer's own find_package(Threads) before find_package(geom) make the error disappear, and why is that not a fix?If the consumer creates `Threads::Threads` first, the name in the targets file resolves. But every consumer would then have to know `geom`'s internal dependencies and call them in the right order. The package config file is the one place that knows them, so `find_dependency` belongs there.
- Why use find_dependency rather than find_package(Threads REQUIRED) inside geomConfig.cmake?A hard-coded `REQUIRED` would abort a consumer who called `find_package(geom)` without `REQUIRED` and wanted to handle absence. `find_dependency` forwards the consumer's `QUIET` and `REQUIRED` choice and, on failure, returns from the config file with a message so `geom` is simply reported as not found.
- install(EXPORT) bundles the dependency packages' targets into the export
- Each consumer should call find_package for geom's dependencies first
- find_dependency belongs after the targets file is included
- A shared library's private dependencies always need find_dependency