123

I'm building a large library using CMake, and I would like users to be able to selectively enable/disable certain parts of my build process.

How can I add command-line options to my CMake build, e.g. so that users may type something like cmake --build-partone --nobuild-parttwo --dothis=true --dothat=false ..?

Apparently the OPTION keyword will create variables that can be set from the CMake GUI, but I can't figure out how to do this from the command line.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
rcv
  • 5,712
  • 9
  • 40
  • 62
  • Thank you for your question as it answered my question with regards to the purpose of cmake option. So, i see now it is for GUI – eigenfield Aug 04 '20 at 18:09

2 Answers2

174

Yeah, you should use the option command. You can set options from the command line this way:

//CMakeLists.txt
option(MyOption "MyOption" OFF)

//Command line
cmake -DMyOption=ON MyProjectFolder

Note that -DMyOption must come before the path.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
beduin
  • 7,473
  • 3
  • 26
  • 24
17

Just a little correction:

If you have other variables to pass, it is recommended to indicate the type of these:

//CMakeLists.txt
option(MyOption "MyOption" OFF)

//Command line
cmake -DMyOption:BOOL=ON MyProjectFolder -D...
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
guilloptero
  • 1,525
  • 1
  • 10
  • 6