What is CMake equivalent of 'configure --prefix=DIR && make all install '?
Solution 1
You can pass in any CMake variable on the command line, or edit cached variables using ccmake/cmake-gui. On the command line,
cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr . && make all install
Would configure the project, build all targets and install to the /usr prefix. The type (PATH) is not strictly necessary, but would cause the Qt based cmake-gui to present the directory chooser dialog.
Some minor additions as comments make it clear that providing a simple equivalence is not enough for some. Best practice would be to use an external build directory, i.e. not the source directly. Also to use more generic CMake syntax abstracting the generator.
mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr .. && cmake --build . --target install --config Release
You can see it gets quite a bit longer, and isn't directly equivalent anymore, but is closer to best practices in a fairly concise form... The --config is only used by multi-configuration generators (i.e. MSVC), ignored by others.
Solution 2
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.
Solution 3
Note that in both CMake and Autotools you don't always have to set the installation path at configure time. You can use DESTDIR at install time (see also here) instead as in:
make DESTDIR=<installhere> install
See also this question which explains the subtle difference between DESTDIR and PREFIX.
This is intended for staged installs and to allow for storing programs in a different location from where they are run e.g. /etc/alternatives via symbolic links.
However, if your package is relocatable and doesn't need any hard-coded (prefix) paths set via the configure stage you may be able to skip it. So instead of:
cmake -DCMAKE_INSTALL_PREFIX=/usr . && make all install
you would run:
cmake . && make DESTDIR=/usr all install
Note that, as user7498341 points out, this is not appropriate for cases where you really should be using PREFIX.
Solution 4
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 theinstalltarget which also builds all necessary dependent targets if they need to be built and then copies the files into theCMAKE_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 --buildcommand is that it works for all generators (i.e. makefiles and Visual Studio) without needing different commands.
Afterwards I use the installed files to create packages or include them in other projects...
Solution 5
Starting with CMake 3.15, the correct way of achieving this would be using:
cmake --install <dir> --prefix "/usr"
Andrei
Updated on August 18, 2022Comments
-
Andrei 2 monthsI 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
cmakeandmakecommand line to install to/usrinstead of/usr/local? -
astrostl over 10 yearsFound this helpful building a package for Mydumper, which uses CMake. Thx! -
albfan almost 10 yearsWondering what :PATH is? It's useful for cmake-gui, helping to choose the widget for that variable. See doc in linux.die.net/man/1/cmake-gui (set section) -
Hal Canary over 9 yearsI think that the:PATHis necessary. -
Marcus D. Hanwell over 9 yearsThey provide hints to the CMake GUI as stated, everything in CMake is effectively a string, but setting PATH, FILEPATH, STRING, BOOL etc help the GUI to present a more appropriate widget. -
RobertJMaynard almost 9 yearsYou can also use: "cmake --build --target install ." instead of make. -
Marcus D. Hanwell almost 9 yearsThat seems longer on Unix, but easier on Windows ;-) The all is not required above, but I left it in as the question asked for the closest equivalent... -
Pedro Lamarão about 8 yearsUsing @RobertJMaynard's suggestion is easier when building with Visual Studio 10 or more recent. -
bodacydo almost 8 yearsWhat is the dot for after /usr?/usr . -
Kamiccolo almost 8 years@bodacydo location of the folder with CMakeLists.txt we're generating from. -
Joe about 7 years@MarcusD.Hanwell the real advantage is platform/generator independence
-
Marcus D. Hanwell about 7 years@Joe I couldn't agree more, but it can help people who come from other tools to see some simple equivalent commands too! -
Shahbaz almost 7 yearsIf I do this (using/usr/localinstead actually), the files still get installed in./install. Has anything changed in newercmake(2.18.12.2)? -
Shahbaz almost 7 yearsNevermind, the project was overridingCMAKE_INSTALL_PREFIXinCMakeLists.txt! -
Joakim over 6 yearsI like showing the use ofDESTDIR. But actually this is wrong. You should refer to the cmake docs cmake.org/cmake/help/v3.0/variable/CMAKE_INSTALL_PREFIX.html ...make DESTDIR=/home/john installwhich will install the concerned software using the installation prefix, e.g. “/usr/local” prepended with the DESTDIR value which finally gives “/home/john/usr/local”. -
Bruce Adams over 6 yearsI don't think that's contradictory. If your package is relocatable you don't need CMAKE_INSTALL_PREFIX, or rather you can choose either method. If it isn't you do because CMAKE_INSTALL_PREFIX will be baked in somewhere at build time. -
kirbyfan64sos about 6 years -
Bruce Adams over 5 yearsThis answer might be better placed as an answer to stackoverflow.com/questions/11307465/destdir-and-prefix-of-make -
helmesjo over 5 yearsThanks for the step by step explanation! IMO this is the only way, otherwise the whole point of cmake (platform independence) is discarded...
-
Slava over 4 yearsrecipe for an in-source build with hardcoded `make' gets ~350 upvotes... Seriously?! -
Slava over 4 yearsdid you forget to include path to sources (../) in line 3? BTW this should be the accepted answer. -
codenamezero over 4 yearsLIne 3 should becmake -G "<generator>" -DCMAKE_INSTALL_PREFIX=stage .. -
Marcus D. Hanwell about 4 yearsIt was asking for equivalence as asked in the question, not best practices. These can be helpful in learning tools. If you read the question this is clearly a reasonable answer, but if I was asked for the best practice then I would certainly advise you to use a separate build directory and the longer/more portable cmake --build syntax... Seriously! -
Dmitry Kabanov over 3 yearsThe answer could have been better if explanation was provided to all the arguments used and why they are used. Particularly, what is the point of the--configargument? -
Cloud over 3 yearsAs an additional note, pragmatically, people usemake -j $(nproc), to specify the number of build threads, docmake --build . --target=install --config=Release -- -j 8for Makefile generator orcmake --build . --target=install --config=Release -- /m:8for Visual Studio generator with 8 threads. Actually, you can pass any commandline parameters after-- -
Mizux over 2 yearsif you know that your generator is Makefile... I prefercmake --build build --target install -- DESTDIR=/usrnote: this should also work with Ninja generator (rules seems to contains$ENV{DESTDIR}) -
Mr Redstoner over 2 years@Joakim as much as I'd like to use CMAKE_INSTALL_PREFIX, doing so embedded the install path in the compiled files. As it happens, I was merely building a .rpm package, so that wouldn't do. DESTDIR worked like a charm for getting things into buildroot. -
Mr Redstoner over 2 years@Cloud sadly -j is not available in all distributions for which Cmake is packaged.-jwas added in 3.12, counting on pkgs.org I'm seeing 17 systems with cmake listed <3.12, for example Ubuntu 18.04 Busy Beaver has 3.10 only. -
Cloud over 2 years@MrRedstoner-jis not a flag for cmake, all flags come after--is passing to the underlying build system... -
Mr Redstoner over 2 years@Cloud wow neat! I though cmake would have such an option, then found the changelog for 3.12 which was newer than mine. Was kinda sad. Didn't realize you could sneak it past cmake in the earlier versions. I shall remember that. -
Woodrow Barlow over 1 year"supported" doesn't mean "correct" -- this has its uses but since building and installing happens more often than generating the build system, it's often safer to just set the env var when generating the build system. both are "correct" depending on use case. -
Alex Reinking over 1 yearDon't use the undocumented-Hflag. The officially supported replacement is-S -
Adam Badura about 1 year@DmitryKabanov CMake supports many generators. However, there are two classes - single configuration ones (like Make and Ninja) and multi-configuration ones (like Visual Studio). The first class relies onCMAKE_BUILD_TYPEestablished during configuration. The second class relies on--configargument provided during building (and then also installing). This is because with multi-configuration generators, unsurprisingly, you can build many types out of single CMake configuration. -
Adam Badura about 1 yearNice! However, you can do more (at least today - I'm not going to check which feature was added when). Firstly, CMake can work "out of source" with-Sand-Barguments on configuration and then--build/--installduring those steps. So, no need to do anycd. Then a purist will add thatcmake -E make_directory <path>replacesmkdirin platform independent way. Then add-DCMAKE_BUILD_TYPE=Releaseto configuration to support single config generators. Finally, skip--target=installand instead invokecmake --install. -
Florian Wolters 12 monthsI've improved my answer. Thanks for the feedback.