20

How do I set CMAKE_INSTALL_PREFIX in my root CMakeLists.txt file?

I have been doing

cmake_minimum_required(VERSION 2.8)
project(MyProject)

# Set default install prefix
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})

with the hopes that by installations would be destined to folders in the source tree. That is,

install(TARGETS my_exe DESTINATION bin/)

would install to ${CMAKE_SOURCE_DIR}/bin/. Instead, it keeps trying to write to /usr/local/bin (the default for Ubuntu 14.04).

I tried the answers to this question, but I still get the standard usr/local/ as my CMAKE_INSTALL_PREFIX when I check CMakeCache.txt.

The only working solution I have is to do

install(TARGETS my_exe DESTINATION "${CMAKE_SOURCE_DIR}/bin/")

but this then removes the user's ability to specify where the bin directory to install is.

tl;dr I would like make install to automatically install to ${CMAKE_SOURCE_DIR} by default, rather than /usr/local/.

Community
  • 1
  • 1
marcman
  • 3,233
  • 4
  • 36
  • 71

1 Answers1

42

CMake developers suggest to use given pattern for change default value of CMAKE_INSTALL_PREFIX inside CMakeLists.txt:

# Use this snippet *after* PROJECT(xxx):
IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment> FORCE)
ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

Using that approach

# Use this snippet *before* PROJECT(xxx):
SET(CMAKE_INSTALL_PREFIX <path> CACHE PATH <comment>)

is not recommended:

.. solution depends on the implementation details of the PROJECT command and is very fragile since it works "accidentally" for some versions of CMake. I don't consider it to be an option at all.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • The key for CMAKE_INSTALL_PREFIX is to use FORCE. – Jav_Rock Dec 22 '16 at 15:55
  • 3
    The reason you wouldn't want to use FORCE to put it in the cache, is because that obliterates any command line option passed in. The suggestion of asking CMake if it's using the default is a very robust solution that won't confuse the user if they try to overwrite it using the command line. – KymikoLoco May 01 '17 at 22:26
  • This doesn't quite work right when you need to change `CMAKE_INSTALL_PREFIX` and run cmake again. It isn't initialized to default on the second run, so the `FORCE` blows away whatever the user is trying to replace it with. – interfect Aug 11 '20 at 20:03
  • @interfect: Hm, it works for me with CMake 3.5, just tested. According to my observations, `CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT` is set only when CMake hasn't found `CMAKE_INSTALL_PREFIX` in the initial cache. That is, on the first `cmake` run when `-D CMAKE_INSTALL_PREFIX=<...>` option hasn't given and on the further `cmake` runs with `-U CMAKE_INSTALL_PREFIX` option. So, `CMAKE_INSTALL_PREFIX` is set to default only in the cases above, as intended. – Tsyvarev Aug 16 '20 at 09:19
  • This has limitations, see https://gitlab.kitware.com/cmake/cmake/-/issues/21242. – Michael-O Sep 25 '20 at 19:44
  • does not work for me – Martin Jan 15 '23 at 09:49