If you try to disable in-source builds, cmake creates CMakeCache.txt and a CMakeFiles folder after the fatal error stops the script. These files then prevent the user from doing an out-of-source build. It’s worthwhile to explain this ridiculous situation to the user, since the script itself can’t solve the problem:
# Do this before project() to skip the compiler config process
if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
message(FATAL_ERROR "In-source builds are disabled.
Please create a subfolder and use `cmake ..` inside it.
NOTE: cmake will now create CMakeCache.txt and CMakeFiles/*.
You must delete them, or cmake will refuse to work.")
endif() # yes, end-markers and even else() need empty parens
At some point (not sure which version) cmake reportedly added a secret setting to disable in-source builds.
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
But this has the same problem (CMakeCache.txt and CMakeFiles are created) and gives users a cryptic error message.
Interestingly my version of cmake (3.11) doesn’t document the -H
option for setting the source folder (or the -B
option for setting the binaries folder):
cmake -H. -Bbuild
In fact, cmake --help
claims that -H
is a synonym for --help
. Nevertheless the command seems to work :)