# Exporting Targets from the Build Tree and immediately using find\_package()

**URL:** https://discourse.cmake.org/t/exporting-targets-from-the-build-tree-and-immediately-using-find-package/10996
**Category:** Code
**Created:** [June 12, 2024, 2:30am UTC](https://discourse.cmake.org/t/exporting-targets-from-the-build-tree-and-immediately-using-find-package/10996 "2024-06-12T02:30:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![huiyan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/d6d6ee/32.png) [@huiyan](https://discourse.cmake.org/u/huiyan)
#### Post date: [June 12, 2024, 2:30am UTC](https://discourse.cmake.org/t/exporting-targets-from-the-build-tree-and-immediately-using-find-package/10996/1 "2024-06-12T02:30:31Z")

</div>

My problem is as follows (cmake version 3.29.4):  
in top CMakeLists.txt has added two subdirectories

```cmake
    add_subdirectory(fmt)
    add_subdirectory(tests)

```

And in file fmt/CMakeLists.txt I export a target by

```cmake
include(CMakePackageConfigHelpers)
configure_package_config_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/install/Config.in.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/cmake/FmtConfig.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/fmt)
write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/cmake/FmtConfigVersion.cmake"
    VERSION "${target_version}"
    COMPATIBILITY AnyNewerVersion)
export(
    EXPORT FmtTargets
    NAMESPACE tests::
    FILE ${CMAKE_BINARY_DIR}/fmt/lib/cmake/FmtTargets.cmake)

```

Then I find\_package(Fmt REQUIRED) in the tests/CMakeLists.txt

```cmake
set(Fmt_DIR ${CMAKE_BINARY_DIR}/fmt/lib/cmake/)
find_package(Fmt REQUIRED)

```

There is FmtConfig.cmake available In directory ${CMAKE\_BINARY\_DIR}/fmt/lib/cmake  
ls ${CMAKE\_BINARY\_DIR}/fmt/lib/cmake has follow output after I run cmake --build

```auto
FmtConfig.cmake FmtConfigVersion.cmake

```

But it raise an error

```auto
[cmake] CMake Error at ${CMAKE_BINARY_DIR}/fmt/lib/cmake/FmtConfig.cmake:27 (include):
[cmake] include could not find requested file:
[cmake] 
[cmake] ${CMAKE_BINARY_DIR}/fmt/lib/cmake/FmtTargets.cmake
[cmake] Call Stack (most recent call first):
[cmake] tests/fmt/CMakeLists.txt:2 (find_package)

```

The problem is FmtTargets.cmake has not generated when call find\_package() in tests/CMakeLists.txt.

If I first build fmt/CMakeLists.txt and then build tests/CMakeLists.txt it works.

Would someon please help me how to make find\_package can find the FmtTargets.cmake after it actually been generated when no seperately running build fmt/CMakeLists.txt first, instead I can run single build command in top CMakeLists.txt ?

Regards,

huiyan

---

<div class="post-metadata">

### Author: ![redbaron](https://discourse.cmake.org/user_avatar/discourse.cmake.org/redbaron/32/2129_2.png) [@redbaron](https://discourse.cmake.org/u/redbaron)
#### Post date: [December 6, 2024, 1:26pm UTC](https://discourse.cmake.org/t/exporting-targets-from-the-build-tree-and-immediately-using-find-package/10996/2 "2024-12-06T13:26:18Z")

</div>

just for tests you probably don’t need any of that, because any target defined in `fmt` are immediately usable in `tests`.

I wanted to do something similar, but for `./example` dir. It should be both a standalone project, but also work fine as `add_subdirectory` in the parent project. As a standalone it should use `find_package(mylib)` and I planned to use `export(EXPORT)`, but it doesn’t get executed immediately: cmake just passes it on proceeds to `find_package`, which then fails like in your case.
