I have a makefile for my project and also I am passing some argument and based on that argument i am seting some flag. I am able to do this . but now I want to do the same using cmake. I have created cmakelist.txt
but I don't know how to pass the argument and check for the argument value in cmakelist.txt
.
Sample of my makefile:
ifeq "$(FAB)" "po" //setting some flags else //setting some iother flag endif
What is the way to take the argument from command prompt and set flags based on that?
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.
Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.
The cmake command creates many files at your current working directory (CWD, the directory you ran the command from), and among them is a file called Makefile , which has rules about which files to compile/build/copy/write/whatever and how to do it.
Use the syntax $ENV{VAR} to read environment variable VAR . To test whether an environment variable is defined, use the signature if(DEFINED ENV{<name>}) of the if() command. For general information on environment variables, see the Environment Variables section in the cmake-language(7) manual.
In the CMakeLists.txt file, create a cache variable, as documented here:
SET(FAB "po" CACHE STRING "Some user-specified option")
Source: http://cmake.org/cmake/help/v2.8.8/cmake.html#command:set
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:
cmake -DFAB:STRING=po
Source: http://cmake.org/cmake/help/v2.8.8/cmake.html#opt:-Dvar:typevalue
Modify your cache variable to a boolean if, in fact, your option is boolean.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With