How to list all CMake build options and their default values?
cmake
Solution
`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://stackoverflow.com/a/53075317/895245 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 CMake
- `CMAKE_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.
Problem
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.