# unset all variables from find\_package()

**URL:** <https://discourse.cmake.org/t/unset-all-variables-from-find-package/2386>\
**Category:** Code\
**Created:** [December 17, 2020, 2:35am UTC](https://discourse.cmake.org/t/unset-all-variables-from-find-package/2386 "2020-12-17T02:35:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [December 17, 2020, 2:35am UTC](https://discourse.cmake.org/t/unset-all-variables-from-find-package/2386/1 "2020-12-17T02:35:26Z")

</div>

Hi,

My task is to allow user to decide whether to use superbuild or manually specify \_DIR to a library.

I’m interested in the situation when user uses CMake-GUI and at first he set `USE_SUPERBUILD = ON` and specify path to a package. After that he configures.  
Then he decided not to use superbuild so he unchecks superbuild option.

In this case I think I need to `unset` all variables that were set by command `find_package()` and somehow delete group-variables from CMake-GUI on the screen.

I don’t see an easy way to do that and I’m not confident that my logic is acceptable.

Snippet of my code:

```cmake
option(SuperBuild_HDF5 "Pull and build HDF5 from Github" ON)

if(NOT SuperBuild_HDF5)
  find_package(HDF5 REQUIRED)
elseif(H5Geo_SB_BUILD_HDF5 AND HDF5_FOUND)
  unset(HDF5_FOUND CACHE) # wrong
endif()

```

---

<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 17, 2020, 11:39am UTC](https://discourse.cmake.org/t/unset-all-variables-from-find-package/2386/2 "2020-12-17T11:39:57Z")

</div>

Knowing the set of variables provided by a `find_package` call is extremely hard to do. I will say that the `_FOUND` variable is usually just a local variable (it certainly doesn’t make sense in the cache at least). I would recommend using imported targets rather than the variables. Most of the variables from a `find_package` are marked as “advanced” anyways and shouldn’t show up in the (default) UI.

In addition, the set of variables for `FindHDF5` is extremely complex as well. So that’s just about the hardest case to handle here 🙂 .

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [December 17, 2020, 2:08pm UTC](https://discourse.cmake.org/t/unset-all-variables-from-find-package/2386/3 "2020-12-17T14:08:19Z")

</div>

Thank you for response

Could you please clarify the text below, I can’t understand it (probably I don’t have much understanding what is difference between target and variable):

> [@ben.boeckel](#):
>
> I would recommend using imported targets rather than the variables.

---

<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 17, 2020, 3:13pm UTC](https://discourse.cmake.org/t/unset-all-variables-from-find-package/2386/4 "2020-12-17T15:13:01Z")

</div>

A custom target would be `hdf5::hdf5` or the like. It’s meant to be used in `target_link_libraries`:

```cmake
target_link_libraries(mytgt PRIVATE hdf5::hdf5)

```

This allows you to avoid using potentially stale variables too (as there’s no way to cache an imported target).

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [December 17, 2020, 4:20pm UTC](https://discourse.cmake.org/t/unset-all-variables-from-find-package/2386/5 "2020-12-17T16:20:51Z")

</div>

> [@ben.boeckel](#):
>
> `target_link_libraries(mytgt PRIVATE hdf5::hdf5)`

And to include `include` dir do I need also use some `HDF5_INCLUDE_DIRS` variable (or some other variable):  
`target_include_directories(mytgt PRIVATE HDF5_INCLUDE_DIRS)`?

---

<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 17, 2020, 4:25pm UTC](https://discourse.cmake.org/t/unset-all-variables-from-find-package/2386/6 "2020-12-17T16:25:58Z")

</div>

Nope, the target comes with “usage requirements” which includes the include directories already.

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [December 17, 2020, 4:26pm UTC](https://discourse.cmake.org/t/unset-all-variables-from-find-package/2386/7 "2020-12-17T16:26:46Z")

</div>

thank you a lot!  
Good to know that 🙂
