What is cmake equivalent of 'configure --prefix=DIR && make all install '?


The ":PATH" part in the accepted answer can be omitted. This syntax may be more memorable:

cmake -DCMAKE_INSTALL_PREFIX=/usr . && make all install

...as used in the answers here.


I do cmake . && make all install.
This works but installs to /usr/local.

I need to install to a different prefix (for example, to /usr).

What is the cmake and make command line to install to /usr instead of /usr/local?

the way I build cmake projects cross platform is the following:

/project-root> mkdir build
/project-root> cd build
/project-root/build> cmake -G "<generator>" -DCMAKE_INSTALL_PREFIX=stage ..
/project-root/build> cmake --build . --target=install --config=Release
  • the first two lines create the out of source build directory
  • the third line generates the build system specifying where to put the installation result (which I always place in ./project-root/build/stage - the path is always considered relative to the current directory if it is not absolute )
  • the fourth line builds the project configured in . with the buildsystem configured in the line before. it will execute the install target which also builds all necessary dependent targets if they need to be built and then copies the files into the CMAKE_INSTALL_PREFIX (which in this case is ./project-root/build/stage. For multi configuration builds like in Visual Studio you can also specify the configuration with the optional --config <config> flag.
  • the good part when using the cmake --build command is that it works for all generators (ie makefiles and visual studio ) without needing different commands.

afterwards I use the installed files to create packages or include them in other projects...

It is considered as bad practice to invoke the actual generator (e.g. via make) if using CMake. It is highly recommended to do it like this:

  1. Configure phase:

    cmake -Hfoo -B_builds/foo/debug -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_INSTALL_PREFIX=/usr
  2. Build and Install phases

    cmake --build _builds/foo/debug --config Debug --target install

When following this approach, the generator can be easily switched (e.g. -GNinja for Ninja) without having to remember any generator-specific commands.