# Exit with error if user attempt to override CMAKE\_INSTALL\_PREFIX during installation

**URL:** <https://discourse.cmake.org/t/exit-with-error-if-user-attempt-to-override-cmake-install-prefix-during-installation/10020>\
**Category:** Code\
**Created:** [February 7, 2024, 5:37am UTC](https://discourse.cmake.org/t/exit-with-error-if-user-attempt-to-override-cmake-install-prefix-during-installation/10020 "2024-02-07T05:37:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kentzo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kentzo/32/4122_2.png) [@Kentzo](https://discourse.cmake.org/u/Kentzo)\
**Post date:** [February 7, 2024, 5:37am UTC](https://discourse.cmake.org/t/exit-with-error-if-user-attempt-to-override-cmake-install-prefix-during-installation/10020/1 "2024-02-07T05:37:12Z")

</div>

My project requires `CMAKE_INSTALL_PREFIX` to be provided during the configuration step. However, cmake allow the user to override it with `cmake --install ... --prefix`. That will result in a broken installation.

Is there a good way to exit with error if the prefix is different between the configuration and installation step. Perhaps there is a way to write `install(CODE ...)` to compare cached value against the current value?

---

<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:** [February 7, 2024, 8:43pm UTC](https://discourse.cmake.org/t/exit-with-error-if-user-attempt-to-override-cmake-install-prefix-during-installation/10020/2 "2024-02-07T20:43:32Z")

</div>

`install(CODE)` sounds like the way to do it to me. `install(SCRIPT)` with a `configure_file()`'d script may be more convenient.

---

<div class="post-metadata">

**Author:** ![Kentzo](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kentzo/32/4122_2.png) [@Kentzo](https://discourse.cmake.org/u/Kentzo)\
**Post date:** [February 7, 2024, 10:41pm UTC](https://discourse.cmake.org/t/exit-with-error-if-user-attempt-to-override-cmake-install-prefix-during-installation/10020/3 "2024-02-07T22:41:03Z")

</div>

Could you provide an example about how to use `install(CODE)` to get this behavior?

---

<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:** [February 8, 2024, 5:16am UTC](https://discourse.cmake.org/t/exit-with-error-if-user-attempt-to-override-cmake-install-prefix-during-installation/10020/4 "2024-02-08T05:16:41Z")

</div>

The pre-`configure_file` code you want there is something like:

```cmake
if (NOT CMAKE_INSTALL_PREFIX STREQUAL "@CMAKE_INSTALL_PREFIX@")
    message(FATAL_ERROR …)
endif ()

```

The in-script code should have a literal on the RHS of that comparison.
