43

I am working on a project which takes considerable time to build (10-15) minutes. I have recompiled to verify if there is a compilation error. Now I want to change the install directory so that I have a new version of executable with the new changes. Is there a method to just modify the install path so that the 'make install' installs to a new location rather than the old one?

A. K.
  • 34,395
  • 15
  • 52
  • 89
  • possible duplicate of [Make install - but not to default directories?](http://stackoverflow.com/questions/3239343/make-install-but-not-to-default-directories) – Tobias Kienzler Nov 01 '13 at 13:28

5 Answers5

63

CMake generated makefiles support the DESTDIR coding convention for makefiles. Thus you can override the default installation location by setting the DESTDIR variable upon invoking make:

$ make install DESTDIR=/opt/local

There is no need to re-run CMake.

sakra
  • 62,199
  • 16
  • 168
  • 151
  • 13
    I just came across this and I don't think that should be the accepted answer. Best should be to re-run cmake with `cmake . -DCMAKE_INSTALL_PREFIX=` which does **not** recompile your project. With using `DESTDIR` files still get prepared with only the default prefix (see generated `install_manifest.txt`). This is particularly bad if you want to uninstall etc. – Marcus Nov 15 '18 at 10:49
  • 1
    This answer has been posted on 21.1.2012, Monday... I tried this on 28.6.2020, Tuesday... I copied this command and modified the folder name... and I pasted it in the terminal.. pressed enter.. and then The beauty is that this still works during 2020... thank you @sakra – user1241241 Apr 28 '20 at 12:34
26

I don't know whether this is generally true, but I can give an example of an application for which the accepted answer by sakra does not work properly. If you modify the install directory by modifying DESTDIR when installing ITK, it will just append DESTDIR to its already formed install path:

make install DESTDIR=/opt/local

[...]

-- Installing: /opt/local/usr/local/lib/cmake/ITK-4.4/WrapITK/Configuration/Typedefs/itkRenyiEntropy[...]

On the other hand, following this answer by Fraser will result in proper install paths without recompilation:

cmake -DCMAKE_INSTALL_PREFIX=/opt/local /path/to/ITK_source
make install

[...]

-- Installing: /opt/local/lib/cmake/ITK-4.4/WrapITK/Configuration/Typedefs/itkRenyiEntropy[...]
Community
  • 1
  • 1
kara deniz
  • 2,396
  • 1
  • 19
  • 15
20

Running CMake with -DCMAKE_INSTALL_PREFIX=<somewhere different to last time> shouldn't cause your project to need recompiled. If you pass other command line parameters to CMake which e.g. alter the compiler flags, that would force a rebuild of affected targets, but simply changing the install prefix won't.

Fraser
  • 74,704
  • 20
  • 238
  • 215
  • Is there a way to change the install prefix without rerunning cmake? It seems that CPACK is capable of doing so, but I cannot see a way for me to do it directly just using make + some variable. If I want to create a package with my own packager, I want to get things under /usr rather than /usr/local, but I'd like that to work without having to ask the programmer to run cmake with the correct CMAKE_INSTALL_PREFIX... – Alexis Wilke Dec 29 '12 at 02:53
  • Well, you could use `set` in your CMakeLists.txt to hard-code the value of `CMAKE_INSTALL_PREFIX`, but I think this is fairly uncommon and might catch out people who are already used to CMake. Maybe a better option would be to print out a `message` in your CMakeLists.txt which displays the current value of `CMAKE_INSTALL_PREFIX` and also gives instructions as to how to change the value. Something like `message("The install path is currently ${CMAKE_INSTALL_PREFIX}")` `message("To change this run: cmake . -DCMAKE_INSTALL_PREFIX=\"\"")` – Fraser Jan 01 '13 at 02:11
10

The canonical definition of DESTDIR and prefix is: the files are installed to $DESTDIR$prefix, but prepared as if their final install location was just $prefix.

So DESTDIR is only for people building packages or tarballs of binaries; CMAKE_INSTALL_PREFIX is for anyone who wants to specify where the built binaries should live in the end.

Dan Kegel
  • 559
  • 5
  • 11
4

Just in case if someone is not using CMake then there is a method to do that in Makefile. If you have Makefile.config file generated in your build directory, find the prefix. This prefix is the install path where binaries/headers etc. will be installed. Changing this will install the binaries/headers to the modified path.

A. K.
  • 34,395
  • 15
  • 52
  • 89