# Preventing "In-Source Builds"

**URL:** <https://discourse.cmake.org/t/preventing-in-source-builds/881>\
**Category:** Development\
**Created:** [March 28, 2020, 10:01pm UTC](https://discourse.cmake.org/t/preventing-in-source-builds/881 "2020-03-28T22:01:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon45792294](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@anon45792294](https://discourse.cmake.org/u/anon45792294)\
**Post date:** [March 28, 2020, 10:01pm UTC](https://discourse.cmake.org/t/preventing-in-source-builds/881/1 "2020-03-28T22:01:52Z")

</div>

In source builds are usually never desire by developers.

“Professional Cmake - A Practial Guide, by Craig Scott” also discourages the practice (Chapter 2.5).

At my company I’ve noticed this function repeated in many many code bases.

```auto
# In source builds cause a lot of problems and unneccessary complexity
function(prevent_in_source_builds)
    if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
        message(FATAL_ERROR "In-source builds are not allowed")
    endif()
endfunction()

```

I think it has enough merrit to be a policy, variable, or standard module.  
Side note this function isn’t perfect. Since it still leaves some artifacts in the source dir…

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [March 30, 2020, 12:38pm UTC](https://discourse.cmake.org/t/preventing-in-source-builds/881/3 "2020-03-30T12:38:11Z")

</div>

Cc: @craig.scott

In order to avoid leaving artifacts in the source directory we need some kind of mark in the source tree that can be checked before reading `CMakeLists.txt` files. See [CMake Issue 6672](https://gitlab.kitware.com/cmake/cmake/-/issues/6672).
