# Find parent project in subprojects using find\_package

**URL:** https://discourse.cmake.org/t/find-parent-project-in-subprojects-using-find-package/6988
**Category:** Usage
**Created:** [November 30, 2022, 4:07pm UTC](https://discourse.cmake.org/t/find-parent-project-in-subprojects-using-find-package/6988 "2022-11-30T16:07:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![fsimonis](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fsimonis/32/1712_2.png) [@fsimonis](https://discourse.cmake.org/u/fsimonis)
#### Post date: [November 30, 2022, 4:07pm UTC](https://discourse.cmake.org/t/find-parent-project-in-subprojects-using-find-package/6988/1 "2022-11-30T16:07:49Z")

</div>

My project consists of a library and example programs.  
The library generates a `mylibrary-config.cmake` in the binary directory using an export set.

My example programs are individual CMake projects calling `find_package(mylibrary MODULE REQUIRED)`.

```nohighlight
mylibrary
├── CMakeLists.txt
└── examples
    ├── a
    │ └── CMakeLists.txt
    └── b
        └── CMakeLists.txt

```

My goal is to build these example programs as part of my build to integrate them into tests.

After creating `mylibrary-config.cmake` in the `CMakeLists.txt` of the library, I use `add_subdirectory` to include the example programs.  
However, this fails as they cannot find `mylibrary-config.cmake`.

Why does this fail, and how can I make my root project visible without modifying the `CMakeLists.txt` of the example programs?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 1, 2022, 6:38pm UTC](https://discourse.cmake.org/t/find-parent-project-in-subprojects-using-find-package/6988/2 "2022-12-01T18:38:09Z")

</div>

The export file is written at generation time (or is partial during the build; I don’t remember exactly). It’s better, IME, to add these as _tests_ rather than `add_subdirectory`. VTK does it [here](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Examples/CMakeLists.txt).

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [December 5, 2022, 1:57pm UTC](https://discourse.cmake.org/t/find-parent-project-in-subprojects-using-find-package/6988/3 "2022-12-05T13:57:05Z")

</div>

Also note that by doing `find_package(mylibrary MODULE REQUIRED)`, you’re explicitly instructing the `find_package` command to use _module_ mode (where it’s looking for `Findmylibrary.cmake` only), while your library is producing a package config file `mylibrary-config.cmake`.

Producing the package config file is the better way of doing things, but you should consume this in a way which supports it; that is, drop the `MODULE` parameter from the `find_package` call.

---

<div class="post-metadata">

### Author: ![fsimonis](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fsimonis/32/1712_2.png) [@fsimonis](https://discourse.cmake.org/u/fsimonis)
#### Post date: [December 5, 2022, 2:59pm UTC](https://discourse.cmake.org/t/find-parent-project-in-subprojects-using-find-package/6988/4 "2022-12-05T14:59:28Z")

</div>

@Angew Thanks for noticing this. I actually meant to write `find_package(mylibrary CONFIG REQUIRED)`.

The library only generates a CMake config, so I enforce finding the config in the example programs.
