# Replace add\_subdirectory with find\_package

**URL:** <https://discourse.cmake.org/t/replace-add-subdirectory-with-find-package/9029>\
**Category:** Code\
**Created:** [September 21, 2023, 5:04am UTC](https://discourse.cmake.org/t/replace-add-subdirectory-with-find-package/9029 "2023-09-21T05:04:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andrew/32/2478_2.png) [@Andrew](https://discourse.cmake.org/u/Andrew)\
**Post date:** [September 21, 2023, 5:04am UTC](https://discourse.cmake.org/t/replace-add-subdirectory-with-find-package/9029/1 "2023-09-21T05:04:18Z")

</div>

I have an environment where multiple products on multiple platforms build using a variety of libraries.

Historically, we have assembled CMake projects with liberal use of add\_subdirectory. I’d prefer to use something based on find\_package instead.

- Avoid using different APIs for adding modules depending on whether we are developing using entire tree or using a package manager (e.g. Conan)
- Avoid assumptions about where and how a particular library target is implemented

Is it possible to set things up so find\_package will work both for modules in the dev tree and installed / packaged modules?

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [September 21, 2023, 12:14pm UTC](https://discourse.cmake.org/t/replace-add-subdirectory-with-find-package/9029/2 "2023-09-21T12:14:26Z")

</div>

Sounds like you want to use [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html). Check out the `FIND_PACKAGE_ARGS` option for `FetchContent_Declare()`. That option is supported with CMake 3.24 or later.

---

<div class="post-metadata">

**Author:** ![friendlyanon](https://discourse.cmake.org/user_avatar/discourse.cmake.org/friendlyanon/32/970_2.png) [@friendlyanon](https://discourse.cmake.org/u/friendlyanon)\
**Post date:** [September 22, 2023, 1:06pm UTC](https://discourse.cmake.org/t/replace-add-subdirectory-with-find-package/9029/3 "2023-09-22T13:06:36Z")

</div>

You can make use of the fact that `find_package` is just an `include` with superpowers. Here is an [example](https://github.com/friendlyanon/cmake-init-fetchcontent) that uses `find_package` in the code, but fetches the dependency using `FetchContent`. This is an opt-in mechanism and developers can just set `CMAKE_MODULE_PATH` to get it. [https://cmake.org/cmake/help/latest/command/find\_package.html:](https://cmake.org/cmake/help/latest/command/find_package.html:)

> **Module mode**
> 
> In this mode, CMake searches for a file called `Find<PackageName>.cmake`, looking first in the locations listed in the [`CMAKE_MODULE_PATH`](https://cmake.org/cmake/help/latest/variable/CMAKE_MODULE_PATH.html#variable:CMAKE_MODULE_PATH) …  
> …  
> When the [basic signature](https://cmake.org/cmake/help/latest/command/find_package.html#basic-signature) is used, the command searches in Module mode first.

You will have to make sure that the build and install interfaces of the dependency match, e.g. if there is no namespaced `ALIAS` target in the dependency’s build interface that matches the exported target name from the install interface, then you will have to bridge the gap to make the integration transparent in your own project’s code.
