How can I list cmake default build option in command-line?
I need to build OpenCV libraries from source. Before that, I want to know what are the default build settings.
cmake -LAH
To list all option(...)
and set(... CACHE ...)
variables, including those marked as advanced, do:
cmake -LAH
where:
-L
: list variables-A
: include advanced variables.CMake marks most but not all of its default pre-defined variables as, and you can mark you own variables as advanced with
mark_as_advanced
). Some default CMake advanced variables include basic default compilation parameters such as:CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/c++ CMAKE_CXX_FLAGS:STRING= CMAKE_CXX_FLAGS_DEBUG:STRING=-g
If you are only interested in the main configuration options of some project, you will likely want to remove the
-A
as it would be mostly noise.-H
: include the help strings as// My help
above each setting.-H
was previously mentioned at: https://mcmap.net/q/209812/-how-to-list-all-cmake-build-options-and-their-default-values consider upvoting that answer
Example
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(ProjectName)
set(GREETING "hello" CACHE STRING "How to greet")
set(GREETING2 "hello2" CACHE STRING "Supersecret greeting")
mark_as_advanced(FORCE VAR GREETING2)
set(MYNOCACHE "mynocacheval")
option(WORLD "Print world or not?" ON)
If we run:
mkdir -p build
cd build
cmake -LAH ..
the output contains:
// How to greet
GREETING:STRING=hello
// Supersecret greeting
GREETING2:STRING=hello2
// How to greet
MSG:STRING=hello
// Supersecret greeting
MSG2:STRING=hello2
// Print world or not?
WORLD:BOOL=ON
plus a bunch of default defined CMake variables. If we remove -A
we get only:
// Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
CMAKE_BUILD_TYPE:STRING=debug
// Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local
// How to greet
GREETING:STRING=hello
// How to greet
MSG:STRING=hello
// Print world or not?
WORLD:BOOL=ON
so we see that our advanced variable GREETING2
is not present. We also understand that CMake feels that two of their default variables are often useful to merit the non-advanced distinction:
CMAKE_BUILD_TYPE
to control debug vs release: Debug vs Release in CMakeCMAKE_INSTALL_PREFIX
to decide where to install the project output
Without -H
we get just:
CMAKE_BUILD_TYPE:STRING=debug
CMAKE_INSTALL_PREFIX:PATH=/usr/local
GREETING:STRING=hello
MSG:STRING=hello
WORLD:BOOL=ON
Note that the values are the cached values, not the defaults. E.g. if we first set a cached value:
cmake -DGREETING=bye ..
then running again cmake -L ..
gives:
CMAKE_BUILD_TYPE:STRING=debug
CMAKE_INSTALL_PREFIX:PATH=/usr/local
GREETING:STRING=bye
MSG:STRING=hello
WORLD:BOOL=ON
Better way to determine the exact build options: run a verbose build!
I think some of people coming to this question are trying to understand how CMake is building their things exactly.
While you could deduce much of that from cmake -LA
, a better more direct and sure-fire way would be as per: Using CMake with GNU Make: How can I see the exact commands? to just build with with:
make VERBOSE=1
The output would then contain a line of type:
/usr/bin/cc -DGREETING=\"hello\" -DWORLD -MD -MT CMakeFiles/main.dir/main.c.o -MF CMakeFiles/main.dir/main.c.o.d -o CMakeFiles/main.dir/main.c.o -c /home/ciro/main.c
which makes it clear exactly what CMake is doing.
ccmake
ncurses
ccmake is an nCurses Cmake UI. The advantage over cmake -L
is that you can interactively set the values as you find them:
sudo apt-get install cmake-curses-gui
ccmake .
shows:
To see the advanced options from cmake-curses-gui
you can just enter the t
key as mentioned on the UI which toggles them on or off.
cmake-gui
This UI is a bit better than ccmake
in terms of capabilities:
sudo apt install cmake-qt-gui
cmake-gui .
we have:
- "Advanced" checkbox to show the advanced options
- "Grouped" checkbox to group options. By default it produces two categories:
- "CMAKE" for the "CMAKE" default options
- "Ungrouped Entries" for your variables Option groups are apparently generated automatically based on common option name prefixes: Creating groups of CMake options
Tested in Ubuntu 22.10, cmake 3.5.2.
cmake -LA
. –
Blouson cmake -D BUILD_SHARED_LIBS=ON
but nothing of proposed above will show it avaliability or status :( –
Wireworm cmake -LAH
with additional information due to the H
flag. –
Restrictive cmake version 3.25.1
, cmake -LAH
outputs the following warning: "No source or binary directory provided. Both will be assumed to be the same as the current working directory, but note that this warning will become a fatal error in future CMake releases.". Therefore, specify the directory. E.g. while in the source root directory, use cmake -LAH .
... or while in a conventional build/
directory, use cmake -LAH ..
–
Nitza You can do cmake -LAH
too. The H
flag will provide you help for each option.
cd build && cmake -LAH ..
. –
Sinasinai I do not know of an direct way to do it.
A way around this is to edit the main CMakeLists.txt and print at the end of the file the settings you are interested. The Variables where the most important cmake setting are stored are listed here:
I always print these variables at the end of my CMakeLists.txt to see the settings.
MESSAGE(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
MESSAGE(STATUS "Library Type: " ${LIB_TYPE})
MESSAGE(STATUS "Compiler flags:" ${CMAKE_CXX_COMPILE_FLAGS})
MESSAGE(STATUS "Compiler cxx debug flags:" ${CMAKE_CXX_FLAGS_DEBUG})
MESSAGE(STATUS "Compiler cxx release flags:" ${CMAKE_CXX_FLAGS_RELEASE})
MESSAGE(STATUS "Compiler cxx min size flags:" ${CMAKE_CXX_FLAGS_MINSIZEREL})
MESSAGE(STATUS "Compiler cxx flags:" ${CMAKE_CXX_FLAGS})
© 2022 - 2024 — McMap. All rights reserved.
cmake-gui
)? – Hodeida