Adding command line options to CMake

Command LineCmake

Command Line Problem Overview


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.

Command Line Solutions


Solution 1 - Command Line

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.

Solution 2 - Command Line

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...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrcvView Question on Stackoverflow
Solution 1 - Command LinebeduinView Answer on Stackoverflow
Solution 2 - Command LineguillopteroView Answer on Stackoverflow