108

I want to generate Makefile with install target, making installation to /usr instead of default /usr/local. Assuming that build directory is done in the source subdirectory, I execute:

cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ..

CMakeCache.txt contains: CMAKE_INSTALL_PREFIX:PATH=/usr (OK?)

Now I execute:

make
make install

All files are still installed to usr/local. What is wrong?

Edit: There is no CMAKE_INSTALL_PREFIX in any of CMakeLists.txt project files. Before running cmake, I delete everything from the output directory. install directives in CMakeLists.txt look like:

install(TARGETS mylibrary DESTINATION lib)

mdoran3844
  • 660
  • 6
  • 14
Alex F
  • 39,172
  • 34
  • 138
  • 200

3 Answers3

131

That should be (see the docs):

cmake -DCMAKE_INSTALL_PREFIX=/usr ..
James
  • 22,556
  • 11
  • 75
  • 125
mtvec
  • 16,168
  • 4
  • 47
  • 79
34

There are two ways to use this variable:

  • passing it as a command line argument just like Job mentioned:

    cmake -DCMAKE_INSTALL_PREFIX=< install_path > ..

  • assigning value to it in CMakeLists.txt:

    SET(CMAKE_INSTALL_PREFIX < install_path >)

    But do remember to place it BEFORE PROJECT(< project_name>) command, otherwise it will not work!

plaes
  • 28,313
  • 10
  • 81
  • 85
ryan_tu
  • 468
  • 4
  • 6
  • 4
    Strange, the SET() statement works for me only if I place it AFTER the PROJECT() statement (CMake 2.8). – AstroFloyd May 04 '15 at 19:00
  • 3
    [This answer](http://stackoverflow.com/a/39485990/1938798) and the reference it links to more directly discusses the before/after the project() command issue. – Craig Scott Nov 19 '16 at 11:04
  • What is this `PROJECT` command and how to have this `cmake` option before it ? You would have an example ? – Stephane Feb 09 '19 at 14:33
  • `set(CMAKE_INSTALL_PREFIX "/usr" CACHE STRING " " FORCE )` , it's ok even if place it after `project()`. – kgbook Mar 05 '21 at 08:44
7

But do remember to place it BEFORE PROJECT(< project_name>) command, otherwise it will not work!

My first week of using cmake - after some years of GNU autotools - so I am still learning (better then writing m4 macros), but I think modifying CMAKE_INSTALL_PREFIX after setting project is the better place.

CMakeLists.txt

cmake_minimum_required (VERSION 2.8)

set (CMAKE_INSTALL_PREFIX /foo/bar/bubba)
message("CIP = ${CMAKE_INSTALL_PREFIX} (should be /foo/bar/bubba")
project (BarkBark)
message("CIP = ${CMAKE_INSTALL_PREFIX} (should be /foo/bar/bubba")
set (CMAKE_INSTALL_PREFIX /foo/bar/bubba)
message("CIP = ${CMAKE_INSTALL_PREFIX} (should be /foo/bar/bubba")

First run (no cache)

CIP = /foo/bar/bubba (should be /foo/bar/bubba
-- The C compiler identification is GNU 4.4.7
-- etc, etc,...
CIP = /usr/local (should be /foo/bar/bubba
CIP = /foo/bar/bubba (should be /foo/bar/bubba
-- Configuring done
-- Generating done

Second run

CIP = /foo/bar/bubba (should be /foo/bar/bubba
CIP = /foo/bar/bubba (should be /foo/bar/bubba
CIP = /foo/bar/bubba (should be /foo/bar/bubba
-- Configuring done
-- Generating done

Let me know if I am mistaken, I have a lot of learning to do. It's fun.

Jim
  • 305
  • 3
  • 5
  • 1
    Try to use FORCE when you do SET: set (CMAKE_INSTALL_PREFIX /foo/bar/bubba CACHE PATH "Cmake prefix" FORCE) – Jav_Rock Dec 22 '16 at 15:57