90

I have a makefile for my project, with which I can pass an argument that controls certain build flags. Now I want to do the same using CMake. I have created CMakeLists.txt but I don't know how to pass the argument and check for the argument value in CMakeLists.txt.

Sample of my Makefile:

ifeq "$(MY_VARIABLE)" "option_value" //setting some flags else //setting some other flag endif 

I then call make using make MY_VARIABLE=option_value. What is the way in CMake to take the argument from command prompt and set flags based on that?

2 Answers 2

103

In the CMakeLists.txt file, create a cache variable, as documented here:

SET(MY_VARIABLE "option_value" CACHE STRING "Some user-specified option") 

Source: https://cmake.org/cmake/help/latest/command/set.html#set-cache-entry

Then, either use the GUI (ccmake or cmake-gui) to set the cache variable, or specify the value of the variable on the cmake command line with -D:

cmake -DMY_VARIABLE:STRING=option_value2 

Modify your cache variable to a boolean if, in fact, your option is boolean.

Sign up to request clarification or add additional context in comments.

4 Comments

If you create the cache variable in the CMakeLists.txt file and then pass the argument via calling cmake, won't the CMakeList.txt file keep overwriting the argument value?
No, the cache is populated on the first run with either the default value, or the value supplied on the command line if it is provided. After the first run, the CMakeLists.txt SET command is ignored and the cache value is used. This allows you to adjust these options and have them persist.
I created a boolean putting SET() in my CMakeList.txt with "false" and then I called the cmake command, with "true". From my experiment the value in SET() sees to be the default?
Whatever is in SET is the default, meaning if not specified on the command line. Command line always overrides, even if cache is already set. If you still have questions please ask a new question
62

CMake 3.13 on Ubuntu 16.04

This approach is more flexible because it doesn't constraint MY_VARIABLE to a type:

$ cat CMakeLists.txt message("MY_VARIABLE=${MY_VARIABLE}") if( MY_VARIABLE ) message("MY_VARIABLE evaluates to True") endif() $ mkdir build && cd build $ cmake .. MY_VARIABLE= -- Configuring done -- Generating done -- Build files have been written to: /path/to/build $ cmake .. -DMY_VARIABLE=True MY_VARIABLE=True MY_VARIABLE evaluates to True -- Configuring done -- Generating done -- Build files have been written to: /path/to/build $ cmake .. -DMY_VARIABLE=False MY_VARIABLE=False -- Configuring done -- Generating done -- Build files have been written to: /path/to/build $ cmake .. -DMY_VARIABLE=1 MY_VARIABLE=1 MY_VARIABLE evaluates to True -- Configuring done -- Generating done -- Build files have been written to: /path/to/build $ cmake .. -DMY_VARIABLE=0 MY_VARIABLE=0 -- Configuring done -- Generating done -- Build files have been written to: /path/to/build 

1 Comment

But typing is good! :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.