# Does \`find\_dependency\` also export target?

**URL:** <https://discourse.cmake.org/t/does-find-dependency-also-export-target/14383>\
**Category:** Code\
**Created:** [July 16, 2025, 7:47pm UTC](https://discourse.cmake.org/t/does-find-dependency-also-export-target/14383 "2025-07-16T19:47:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rturrado](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rturrado/32/3499_2.png) [@rturrado](https://discourse.cmake.org/u/rturrado)\
**Post date:** [July 16, 2025, 7:47pm UTC](https://discourse.cmake.org/t/does-find-dependency-also-export-target/14383/1 "2025-07-16T19:47:55Z")

</div>

Hi all,

I am playing with these two blocks of CMake code (I replaced some target names with `...`).

1. `find_dependency`

```auto
include(CMakeFindDependencyMacro)
find_dependency(nlohmann_json)
export(TARGETS ${MLIR_EXPORTS} ... FILE ${catalyst_cmake_builddir}/CatalystTargets.cmake)

```

1. `find_package`

```auto
find_package(nlohmann_json CONFIG REQUIRED PATHS ${nlohmann_json_BINARY_DIR})
export(TARGETS ${MLIR_EXPORTS} ... FILE ${catalyst_cmake_builddir}/CatalystTargets.cmake)

```

The first block compiles fine, but the second block complains of `nlohmann_json` not being exported. `nlohmann_json` is retrieved via FetchContent in an outer CMakeLists.txt.

Shouldn’t `find_dependency` be just a light wrapper around `find_package`?  
Or is it doing something else?  
I’ve read about `EXPORT_PACKAGE_DEPENDENCIES` but that option doesn’t seem to be used in my codebase.

Many thanks,  
Roberto.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [July 16, 2025, 8:38pm UTC](https://discourse.cmake.org/t/does-find-dependency-also-export-target/14383/2 "2025-07-16T20:38:26Z")

</div>

see [FindDependency](https://cmake.org/cmake/help/latest/module/CMakeFindDependencyMacro.html) docs.

Both blocks do not find the package, because it is not yet installed I guess.

---

<div class="post-metadata">

**Author:** ![rturrado](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rturrado/32/3499_2.png) [@rturrado](https://discourse.cmake.org/u/rturrado)\
**Post date:** [July 17, 2025, 6:17pm UTC](https://discourse.cmake.org/t/does-find-dependency-also-export-target/14383/3 "2025-07-17T18:17:42Z")

</div>

These are the outputs I get for 3 different cases.

I am looking for an explanation about cases 2 and 3. Why DON’T I need to export `nlohmann_json` after finding it via `find_dependency` but I DO need to export it after finding it via `find_package`? It looks like `find_dependency` is doing something else apart from merely light wrapping `find_package`.

**Case 1**  
`nlohmann_json` package is found.  
It is NOT included in the `export TARGETS` list.  
It is NOT found by other targets.

_Code_

```auto
find_package(nlohmann_json CONFIG REQUIRED PATHS ${nlohmann_json_BINARY_DIR})
export(TARGETS ${MLIR_EXPORTS} QECUtils libstim tomlplusplus_tomlplusplus FILE ${catalyst_cmake_builddir}/CatalystTargets.cmake)

```

_Output_  
Errors: targets cannot find `nlohmann_json` export.  
It does not buiild.

```auto
CMake Error in cmake/modules/CMakeLists.txt:
  export called with target "qec-transforms" which requires target
  "nlohmann_json" that is not in any export set.

CMake Error in cmake/modules/CMakeLists.txt:
  export called with target "ion-transforms" which requires target
  "nlohmann_json" that is not in any export set.

```

**Case 2**  
`nlohmann_json` package is found.  
It is included in the `export TARGETS` list.  
It is found by other targets.

**Code**

```auto
find_package(nlohmann_json CONFIG REQUIRED PATHS ${nlohmann_json_BINARY_DIR})
export(TARGETS ${MLIR_EXPORTS} nlohmann_json QECUtils libstim tomlplusplus_tomlplusplus FILE ${catalyst_cmake_builddir}/CatalystTargets.cmake)

```

_Output_  
No warnings, no errors.  
It builds.

**Case 3**  
`nlohmann_json` package is found via `find_dependency`.  
It is NOT included in the `export TARGETS` list.  
It is found by other targets.

_Code_

```auto
include(CMakeFindDependencyMacro)
find_dependency(nlohmann_json)
export(TARGETS ${MLIR_EXPORTS} QECUtils libstim tomlplusplus_tomlplusplus FILE ${catalyst_cmake_builddir}/CatalystTargets.cmake)

```

_Output_  
Warning: `find_dependency` cannot find the `nlohmann_json` config file.  
It builds.

```auto
CMake Warning at /usr/share/cmake-3.28/Modules/CMakeFindDependencyMacro.cmake:76 (find_package):
  By not providing "Findnlohmann_json.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "nlohmann_json", but CMake did not find one.

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

    nlohmann_jsonConfig.cmake
    nlohmann_json-config.cmake

  Add the installation prefix of "nlohmann_json" to CMAKE_PREFIX_PATH or set
  "nlohmann_json_DIR" to a directory containing one of the above files. If
  "nlohmann_json" provides a separate development package or SDK, be sure it
  has been installed.
Call Stack (most recent call first):
  cmake/modules/CMakeLists.txt:39 (find_dependency)

```

---

<div class="post-metadata">

**Author:** ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)\
**Post date:** [July 17, 2025, 8:20pm UTC](https://discourse.cmake.org/t/does-find-dependency-also-export-target/14383/4 "2025-07-17T20:20:58Z")

</div>

Case 3 doesn’t perform the `export()` at all, `find_dependency()` is a macro which calls `return()` if it fails to find the desired target. The processing of the CML ends at that point, the `export()` is never reached.

You should never use `find_dependency()` inside a CML like this, it is only intended to be called inside a CMake package configuration file

---

<div class="post-metadata">

**Author:** ![rturrado](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rturrado/32/3499_2.png) [@rturrado](https://discourse.cmake.org/u/rturrado)\
**Post date:** [July 17, 2025, 9:03pm UTC](https://discourse.cmake.org/t/does-find-dependency-also-export-target/14383/5 "2025-07-17T21:03:05Z")

</div>

Many thanks for the explanation @vito.gamberini.

Do you see any problems using the code from Case 2?

---

<div class="post-metadata">

**Author:** ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)\
**Post date:** [July 17, 2025, 9:16pm UTC](https://discourse.cmake.org/t/does-find-dependency-also-export-target/14383/6 "2025-07-17T21:16:27Z")

</div>

The use of `export()` at all is at little concerning. Assuming you have a valid use case for consuming non-utility targets from a build tree, it’s fine. LLVM-adjacent things like MLIR are always weird about their desire to be consumed from build trees. It’s not idiomatic CMake exactly, but it might be idiomatic for LLVM-based code.
