0

I stumbled upon the following nice cmake feature recently:

https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT.html

Save the following snippet as CMakeLists.txt, run mkdir build; cd build:

cmake_minimum_required(VERSION 3.12)

message(STATUS "CMID: ${CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT}")

project( test )

Executing that snippet does not output a true variable:

➜  build /usr/bin/rm -rf *; cmake ../ | grep CMID
-- CMID: 

Now if you change that snippet:

cmake_minimum_required(VERSION 3.12)

project( test )

message(STATUS "CMID: ${CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT}")

Execution yields the result, I expect from the documentation:

➜  build /usr/bin/rm -rf *; cmake ../ | grep CMID
-- CMID: 1

So I wonder, how does the relative position of the project( ... ) command change that variable?

choeger
  • 3,562
  • 20
  • 33

1 Answers1

1

project() call sets many CMake variables, and CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT is one of such variables.

So, for many CMake commands and variables' accesses placing them before or after the project() call is crusial.

In most cases, project() call should be before the using of the other commands and variables.


If you are looking for a way of changing default install prefix from the CMakeLists.txt, see that my answer: https://stackoverflow.com/a/39485990/3440745.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153