# Best practices when providing CMake functions in a library

**URL:** https://discourse.cmake.org/t/best-practices-when-providing-cmake-functions-in-a-library/2092
**Category:** Usage
**Created:** [October 29, 2020, 11:59am UTC](https://discourse.cmake.org/t/best-practices-when-providing-cmake-functions-in-a-library/2092 "2020-10-29T11:59:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![eiband](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/e/aca169/32.png) [@eiband](https://discourse.cmake.org/u/eiband)
#### Post date: [October 29, 2020, 11:59am UTC](https://discourse.cmake.org/t/best-practices-when-providing-cmake-functions-in-a-library/2092/1 "2020-10-29T11:59:04Z")

</div>

My library has CMake functions which add build steps of a code generator to a target. I would like users of my library to be able to use these functions. However I’m unsure how I should provide them. The most simple solution is a CMake include. A library user however must know the path or the `CMAKE_MODULE_PATH` has to be set accordingly.

Question 1: Is it possible/best practice to provide `CMAKE_MODULE_PATH` which points to the includes of the library in the CMake package, so that users of the library can include my files after `find_package()`?

Question 2: Is it possible/best practice to make the functions directly available with `find_package()`?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [October 29, 2020, 12:54pm UTC](https://discourse.cmake.org/t/best-practices-when-providing-cmake-functions-in-a-library/2092/2 "2020-10-29T12:54:51Z")

</div>

The classical way to proceed is to deliver directly the functions when `find_package()` is evaluated.  
To avoid any potential clash names, it is **strongly** recommended prefixing the functions with the name of the package:

```cmake
find_package(MyPackage ...)

# use function created by the package
MyPackage_MyFunction(...)

```

---

<div class="post-metadata">

### Author: ![jairo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jairo/32/672_2.png) [@jairo](https://discourse.cmake.org/u/jairo)
#### Post date: [February 1, 2023, 2:14pm UTC](https://discourse.cmake.org/t/best-practices-when-providing-cmake-functions-in-a-library/2092/3 "2023-02-01T14:14:21Z")

</div>

my apologies for reviving an old thread, but could someone explain (or point me to a resource that explains) how one would “_deliver directly the functions when `find_package()` is evaluated_”?`My assumption is that the .cmake files with the functions definitions would be loaded (via`include()`? ) into the scope of the `find\_package()` caller. If so, I’m still unclear how that would be implemented.

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [February 1, 2023, 2:32pm UTC](https://discourse.cmake.org/t/best-practices-when-providing-cmake-functions-in-a-library/2092/4 "2023-02-01T14:32:19Z")

</div>

The functions are always **global** , so any function defined in a file evaluated through `find_package()` will be available globally.

Moreover, the module file loaded through `find_package()` is evaluated in the scope of the caller. So, any variable set or updated as part of this evaluation will be visible by the caller.

---

<div class="post-metadata">

### Author: ![jairo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jairo/32/672_2.png) [@jairo](https://discourse.cmake.org/u/jairo)
#### Post date: [February 1, 2023, 6:10pm UTC](https://discourse.cmake.org/t/best-practices-when-providing-cmake-functions-in-a-library/2092/5 "2023-02-01T18:10:26Z")

</div>

thank you that was very helpful.

I was also avoiding having to create a `config.cmake.in` file and the `include(CMakePackageConfigHelpers)` boilerplate. Unfortunately, without the config.cmake.in file, it wasn’t obvious how I could `include()` the .cmake files in questions. Adding the .config file as shown in [CMake tutorial, step 11](https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20Export%20Configuration.html) helped.
