# FetchContent\_MakeAvailable in export configuration

**URL:** https://discourse.cmake.org/t/fetchcontent-makeavailable-in-export-configuration/10054
**Category:** Usage
**Created:** [February 11, 2024, 11:22am UTC](https://discourse.cmake.org/t/fetchcontent-makeavailable-in-export-configuration/10054 "2024-02-11T11:22:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![olivierbichler-cea](https://discourse.cmake.org/user_avatar/discourse.cmake.org/olivierbichler-cea/32/3668_2.png) [@olivierbichler-cea](https://discourse.cmake.org/u/olivierbichler-cea)
#### Post date: [February 11, 2024, 11:22am UTC](https://discourse.cmake.org/t/fetchcontent-makeavailable-in-export-configuration/10054/1 "2024-02-11T11:22:14Z")

</div>

Hello,

Say I want to create an export configuration for a library with a dependency provided by `FetchContent_MakeAvailable`, like the following:

```auto
Include(FetchContent)

FetchContent_Declare(
    fmt
    GIT_REPOSITORY https://github.com/fmtlib/fmt.git
    GIT_TAG 10.2.1 # or a later release
)

FetchContent_MakeAvailable(fmt)

...

target_link_libraries(${module_name} PRIVATE fmt::fmt)

```

How should I write my export configuration file? Is it good practice to simply use `FetchContent_MakeAvailable` also in the config file like this:

```auto
@PACKAGE_INIT@
Include(FetchContent)

FetchContent_Declare(
    fmt
    GIT_REPOSITORY https://github.com/fmtlib/fmt.git
    GIT_TAG 10.2.1 # or a later release
)

FetchContent_MakeAvailable(fmt)

include(${CMAKE_CURRENT_LIST_DIR}/mylib-config-version.cmake)

include(${CMAKE_CURRENT_LIST_DIR}/mylib-targets.cmake)

```

?

My worry is that the fetched repository version for the exported configuration may be different that the original one used for building my library (if the repository was updated between the original build of my library and its import in another project). Is there a solution / another way to handle this?

Thanks

---

<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: [February 11, 2024, 6:32pm UTC](https://discourse.cmake.org/t/fetchcontent-makeavailable-in-export-configuration/10054/2 "2024-02-11T18:32:34Z")

</div>

I may do it like at the [ModernCppStarter](https://github.com/TheLartians/ModernCppStarter/blob/master/CMakeLists.txt) project.

---

<div class="post-metadata">

### Author: ![olivierbichler-cea](https://discourse.cmake.org/user_avatar/discourse.cmake.org/olivierbichler-cea/32/3668_2.png) [@olivierbichler-cea](https://discourse.cmake.org/u/olivierbichler-cea)
#### Post date: [February 12, 2024, 8:45am UTC](https://discourse.cmake.org/t/fetchcontent-makeavailable-in-export-configuration/10054/3 "2024-02-12T08:45:28Z")

</div>

Actually, the correct solution seems more to install the dependency like any other target like this:

```auto
install(TARGETS fmt EXPORT ${project}-targets
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

```

According to [https://stackoverflow.com/questions/71541589/how-to-exporting-targets-fetched-with-fetchcontent](https://stackoverflow.com/questions/71541589/how-to-exporting-targets-fetched-with-fetchcontent)

But I get an

```auto
The following imported targets are referenced, but are missing:
  fmt::fmt-header-only

```

when I try to use my package in another project.

---

<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: [February 12, 2024, 9:54am UTC](https://discourse.cmake.org/t/fetchcontent-makeavailable-in-export-configuration/10054/4 "2024-02-12T09:54:02Z")

</div>

```cmake
CPMAddPackage(
  NAME fmt
  GIT_TAG 10.2.1
  GITHUB_REPOSITORY fmtlib/fmt
  SYSTEM ON # used in case of cmake v3.25
  OPTIONS "FMT_INSTALL YES" # create an installable target
)

```

### Most projects does install itself only configured as subproject!

---

<div class="post-metadata">

### Author: ![olivierbichler-cea](https://discourse.cmake.org/user_avatar/discourse.cmake.org/olivierbichler-cea/32/3668_2.png) [@olivierbichler-cea](https://discourse.cmake.org/u/olivierbichler-cea)
#### Post date: [February 12, 2024, 10:28am UTC](https://discourse.cmake.org/t/fetchcontent-makeavailable-in-export-configuration/10054/5 "2024-02-12T10:28:41Z")

</div>

Thanks, but for some reason, even using `CPMAddPackage`, I still got the following error when I try to import my package in another project:

```auto
CMake Error at CMakeLists.txt:33 (find_package):
  Found package configuration file:

    ......./install/lib/cmake/mylib/mylib-config.cmake

  but it set mylib_FOUND to FALSE so package "mylib" is considered
  to be NOT FOUND. Reason given by package:

  The following imported targets are referenced, but are missing: fmt::fmt

```

Actually, an installable target is indeed created for fmt, in `install/cmake/lib/cmake/fmt`, along with `install/cmake/lib/cmake/mylib`.

But in a new project, I would expect that find\_package(mylib) would handle this dependency. Perhaps I am doing something wrong in the install config of mylib?

---

<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: [February 12, 2024, 11:19am UTC](https://discourse.cmake.org/t/fetchcontent-makeavailable-in-export-configuration/10054/6 "2024-02-12T11:19:22Z")

</div>

> **[GitHub - ClausKlein/ModernCppStarter: 🚀 Kick-start your C++! A template for...](https://github.com/ClausKlein/ModernCppStarter)**
>
> 🚀 Kick-start your C++! A template for modern C++ projects using CMake, CI, code coverage, clang-format, reproducible dependency management and much more. - GitHub - ClausKlein/ModernCppStarter: 🚀 K...

It works, try it out please:

```bash
cmake --worklow --preset default
pushd test
cmake --worklow --preset default

```

Where is your fmt package installed?
