101

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.

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
Haris
  • 13,645
  • 12
  • 90
  • 121

3 Answers3

114

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:

enter image description here

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 .

enter image description here

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
33

You can do cmake -LAH too. The H flag will provide you help for each option.

Amir
  • 10,600
  • 9
  • 48
  • 75
  • This only lists options as a by-product of doing many other things, for example if called like this it would clutter the root folder with extra files. Better use in form `cd build && cmake -LAH ..`. – scrutari Feb 11 '20 at 13:33
2

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})
tune2fs
  • 7,605
  • 5
  • 41
  • 57
  • a more detail answer can be found here : https://stackoverflow.com/questions/24767450/using-cmake-how-do-you-determine-the-default-compiler-flags-per-build-type – Marine Galantin Dec 06 '20 at 15:31