I want to use the cmake --build command to build my project.
This command has a --config option. I don't know how many different parameters I can assign to. And I found cmake doesn't check if the parameter of --config is correct or not
You can call cmake --build like this:
cmake --build . --target MyExe --config Debug
This would be run from your build root, since the directory is passed as ., and would build the target MyExe in Debug mode.
If your build tool is a multi-configuration one (like devenv on Windows), the --config argument matters. If you pass an invalid parameter as the config type here, the build tool should give an error.
If the build tool isn't multi-config (like gcc), then the --config argument is ignored. Instead the build type is set via the CMAKE_BUILD_TYPE CMake variable; i.e. it's set when running CMake, not when running the build tool.
You can pass further options to the build tool by adding them at the end after a --, e.g to pass -j4 if using gcc:
cmake --build . --target MyExe -- -j4
Related
I run cmake to generate a VS2019 project:
> cmake -B Debug -S . --log-level=TRACE -G "Visual Studio 16 2019" -A Win32
I build it with:
> cmake --build Debug
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
<snip>
I'd like to see the exact command line CMake is executing to actually start the build engine.
using cmake --build Debug -v outputs more information but still doesn't show how the Build Engine was launched.
I have also tried this, which failed:
>cmake --debug-output --trace --build Debug
Running with debug output on.
Running with trace output on.
CMake Error: Unknown argument --build
CMake Error: Run 'cmake --help' for all supported options.
putting --debug-output and --trace after --build also fails:
>cmake --build Debug --debug-output --trace
Unknown argument --debug-output
Usage: cmake --build <dir> [options] [-- [native-options]]
cmake --build --preset <preset> [options] [-- [native-options]]
Options:
<snip>
How can I get cmake --build to output the exact command (exe & arguments) that is being used for building? And how do I affect it?
For Visual Studio generator CMake uses MSBuild (or whatever the generator is configured for, actually, i think it's kind of "implementation-defined" feature, which is not supposed to be exposed to the users).
You can find full list of MSBuild command-line options here. In order to provide the build tool with the said options use <build-tool-options> of CMake's --build command. E.g. here is (quite verbose) logging:
cmake --build Debug -- -v:d
How can I get cmake --build to output the exact command (exe &
arguments) that is using for building?
As I proposed originally you better just live with the abstractions provided by CMake, but one trick that you may find handy (and it kind of works independently from the build tool) is to try to specify some garbarge options for the tool, so it triggers error and exposes some details:
cmake --build Debug -- garbarge
In this case MSBuild actually prints exactly the command which failed (with original + garbarge arguments):
MSBUILD : error MSB1008: Only one project can be specified.
Full command line: '"C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe" ALL_BUILD.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:m garbarge'
With CMake version 3.19.2 I can use --target argument to build specific targets, instead of all.
For example --target tests to build tests.
However, with CMake 3.22.1 I'm getting an error like this:
CMake Error: Unknown argument --target
CMake Error: Run 'cmake --help' for all supported options.
You can see the manual of CMake here:
https://cmake.org/cmake/help/latest/manual/cmake.1.html
(There is a drop-down list for version selection)
It describes the --target argument, and It doesn't seem any different from what it was earlier. Nonetheless, after switching from 3.19.2 to 3.22.1 it doesn't let me use --target.
#EDIT thank you for your feedback, here's what I use:
cmake -G Ninja -DCROSS_COMPILER_PREFIX=<some_prefix> -Dsomeothervariables=1 --target tests $directory_with_cmake_project
It works with 3.19.2, but executing the same thing with cmake 3.22.1 causes the error.
I expect that the order of providing -G Ninja, variables, target directory and --target matters, but I haven't managed to get it to work in any order I could think of.
CMake is composed of stages - first you configure the project, then you build it:
configuration stage cmake <sourcedir> ... https://cmake.org/cmake/help/v3.22/manual/cmake.1.html#generate-a-project-buildsystem
build stage cmake --build <builddir> ... https://cmake.org/cmake/help/v3.22/manual/cmake.1.html#build-a-project
--target argument if for the build stage, and it is invalid for configuration stage, hence your error.
CMake prints this message, if you haven't specified a directory to build by passing --build <some dir> first. (The --target option only if mentioned in this usage version of the cmake command line tool, see the documentation.)
Wrong
cmake --target foo
CMake Error: Unknown argument --target
CMake Error: Run 'cmake --help' for all supported options.
Correct
(assuming in the subdirectory build of the current working directory contains a configured cmake project with a target foo).
cmake --build build --target foo
All the build hierarchy of my project is based on ExternalProject with --config option. A few days ago I updated to cmake 3.20, and now --config is gone:
$ cmake --config
CMake Error: Unknown argument --config
CMake Error: Run 'cmake --help' for all supported options.
while documentation still advises to use it. Release notes are also silent about the option.
What should I use instead of --config?
You should use for configure
cmake -S . `-D CMAKE_BUILD_TYPE=Release`
CMAKE_BUILD_TYPE will be ignored at configure
And for build
cmake --build . --config Release
Based on https://stackoverflow.com/a/64719718
I don't understand why docs ignored this movement
Based on github Issue you should use
cmake --build .
OK I had the same problem on macOS with the XCode generator.
Turns out all I was missing was the --build command.
i.e. instead of:
cmake . --config Debug
I have to type:
cmake --build . --config Debug
With the cmake generator "Ninja Multi-Config" what is the proper way to handle CMAKE_INSTALL_PREFIX. For instance, if you do:
$ cmake -DCMAKE_INSTALL_PREFIX=../install -G "Ninja Multi-Config" ..
$ cmake --build . --config Release --target install
And then afterwards do
$ cmake --build . --config Debug --target install
will the files in ../install be overwritten by the Debug install? What is the normal way to handle the install location in such cases?
By default they will be overwritten- Ie. with multi-config, the configurations' files install to the same locations.
Command-line "manual" approach
If you don't mind having to do this kind of thing manually on the command-line each time you install, you can just use the --prefix parameter for cmake --install <...>.
From the docs for CMAKE_INSTALL_PREFIX:
The CMAKE_INSTALL_PREFIX may be defined when configuring a build tree to set its installation prefix. Or, when using the cmake(1) command-line tool's --install mode, one may specify a different prefix using the --prefix option.
In that sense, CMAKE_INSTALL_PREFIX can be seen as a default value set per-generated buildsystem that can be overridden on the commandline.
So you can do something like cmake --install <build_dir> --config <config> --prefix <install_dir_unique_to_config>.
defaults in CMakeLists.txt approach
See this CMake mailing thread for various workarounds. Summarized here:
You can (with some exceptions- see the docs) use the <CONFIG>_POSTFIX target property to append a postfix to an output name of a target.
set_target_properties(my_target <more targets can be listed here> PROPERTIES
DEBUG_POSTFIX "-debug"
RELEASE_POSTFIX "-release"
# etc.
)
Workaround using install(DESTINATION) parameters:
install(TARGETS ${LIB_NAME}
CONFIGURATIONS DEBUG
EXPORT ${LIB_NAME}Config-d
PUBLIC_HEADER DESTINATION "include/${LIB_NAME}"
LIBRARY DESTINATION "bin/${LIB_NAME}/debug/"
ARCHIVE DESTINATION "lib/${LIB_NAME}/debug"
)
install(TARGETS ${LIB_NAME}
CONFIGURATIONS RELEASE
EXPORT ${LIB_NAME}Config
PUBLIC_HEADER DESTINATION "include/${LIB_NAME}"
LIBRARY DESTINATION "bin/${LIB_NAME}/release/"
ARCHIVE DESTINATION "lib/${LIB_NAME}/release/"
)
I would like to setup commands like make debug, make test, etc... What is the best way to do this with cmake, so that I run cmake .. one time (it takes a while) and then be able to choose the build type with make?
I couldn't find any resources on this.
As #Tsyvarev has commented this needs a little extra work for single-configuration environments (respectively CMake's Makefile generators) since the build type is chosen during CMake's configuration and finalized during the build environment generation step.
So here is what I've done:
First you run - e.g. in a script - CMake for all configurations you want to support and choose respective sub-folders for the output:
> cmake -H"." -B"Debug" -DCMAKE_BUILD_TYPE=Debug
> cmake -H"." -B"Release" -DCMAKE_BUILD_TYPE=Release
Note: -H (for "home directory") and -B (for "binary output directory") are undocumented options, but very useful in those cases. And they work with all CMake releases so far. Just be careful not to put spaces between the option and the their values.
Then you can use again CMake to build from/in those sub-folders:
> cmake --build "Debug"
> cmake --build "Release"
And if you want to run the tests you can:
> cmake --build "Debug" --target "test"
> cmake --build "Release" --target "test"
References
Changing CMake files standard location
Does CMake always generate configurations for all possible project configurations?
cmake build multiple targets in different build directories
CMAKE_BUILD_TYPE not being used in CMakeLists.txt