# configure\_file vs configure\_package\_config\_file

**URL:** https://discourse.cmake.org/t/configure-file-vs-configure-package-config-file/10993
**Category:** Usage
**Created:** [June 11, 2024, 8:56pm UTC](https://discourse.cmake.org/t/configure-file-vs-configure-package-config-file/10993 "2024-06-11T20:56:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tomhh](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tomhh/32/493_2.png) [@tomhh](https://discourse.cmake.org/u/tomhh)
#### Post date: [June 11, 2024, 8:56pm UTC](https://discourse.cmake.org/t/configure-file-vs-configure-package-config-file/10993/1 "2024-06-11T20:56:51Z")

</div>

Hi there,

Very quick question, I just wanted to check is it always preferred to use `configure_package_config_file` as opposed to `configure_file` when doing anything with package config files (this is for the main package and any other internal components too). If this is the case, is it also recommended to have every `<package>-config.cmake.in` also start with `@PACKAGE_INIT@`? I think that’s the case, but I just wanted to double check.

Thanks very much for your help,

Cheers!

Tom

---

<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: [June 11, 2024, 10:25pm UTC](https://discourse.cmake.org/t/configure-file-vs-configure-package-config-file/10993/2 "2024-06-11T22:25:36Z")

</div>

You only need `configure_package_config_file()` if you need to refer to a path within your installed directory layout, and that path isn’t trivially available using something like `${CMAKE_CURRENT_LIST_DIR}/blah`. It is primarily useful when you need a path relative to the base of the install, and where that path is defined by a variable like `CMAKE_INSTALL_LIBDIR`. Many projects don’t need to do this, and they can get away with plain old `configure_file()`.

If you do use `configure_package_config_file()`, then you must put `@PACKAGE_INIT@` at the top of it. That is the part that defines variables that the rest of your file will need (the `@PACKAGE_BLAHBLAH@` variables that correspond to the `PATH_VARS` arguments).

---

<div class="post-metadata">

### Author: ![tomhh](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tomhh/32/493_2.png) [@tomhh](https://discourse.cmake.org/u/tomhh)
#### Post date: [June 13, 2024, 7:27am UTC](https://discourse.cmake.org/t/configure-file-vs-configure-package-config-file/10993/3 "2024-06-13T07:27:37Z")

</div>

Thank you very much for the swift response @craig.scott, much appreciated!

I see, that’s good to know. I wound up switching to `configure_package_config_file()` mainly for consistency in the context I’m using it, but I might consider switching back if it’s simpler.

I have a `config.in` file that looks more or less like this:

```auto
include(CMakeFindDependencyMacro)
find_dependency(my-dependency)
include(${CMAKE_CURRENT_LIST_DIR}/my-library-targets.cmake) 

```

And was calling it from `CMakeLists.txt` like so:

```auto
configure_file(my-library-config.cmake.in my-library-config.cmake @ONLY)

```

And decided to update it to:

```auto
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
find_dependency(my-dependency)
include(${CMAKE_CURRENT_LIST_DIR}/my-library-targets.cmake)

```

And then call it like this:

```auto
configure_package_config_file(
  my-library-config.cmake.in my-library-config.cmake
  INSTALL_DESTINATION
    ${CMAKE_INSTALL_LIBDIR}/cmake/my-project)

```

Both work, and maybe the first way was okay, but I guess in theory if the `config.in` file ever grows/changes and needs to refer to other paths, it’s now ready to do so.

Thanks again for your help and advice!
