[CMake] Manually-specified variables were not used by the project

Alan W. Irwin Alan.W.Irwin1234 at gmail.com
Fri May 24 16:58:19 EDT 2019


On 2019-05-24 19:38+0200 Haio Maio wrote:

> Manually-specified variables were not used by the project
> MY_VAR
>
> when I call
>
> cmake -DMY_VAR= ...
>
>
> The goal is
> if(NOT DEFINED MY_VAR)
> ...
> else()
> ...
> if()
>
> in order to tell cmake over the command line what to do.
>
> How to achieve this ?

This functionality just works for me (using cmake version 3.13.4).

For example, here is a simple test case:

irwin at merlin> cat /tmp/CMakeLists.txt 
if(NOT DEFINED MY_VAR)
   message(STATUS "MY_VAR is not DEFINED")
else()
   message(STATUS "MY_VAR is DEFINED")
endif()

irwin at merlin> cmake -P /tmp/CMakeLists.txt 
-- MY_VAR is not DEFINED
irwin at merlin> cmake -DMY_VAR=OFF -P /tmp/CMakeLists.txt 
-- MY_VAR is DEFINED
irwin at merlin> cmake -DMY_VAR=ON -P /tmp/CMakeLists.txt 
-- MY_VAR is DEFINED

So just like the documentation says at <https://cmake.org/cmake/help/latest/command/if.html>
what is tested is whether *any* true or false value has been set.  What is more problematic
is how to make sure the variable is not set with the -D option, and the only way I
know how to do that is the first test above (i.e., do not use -DMY_VAR=??? at all).

I hope this simple example helps you to figure out what is going on with your more complex
example.

Alan
__________________________
Alan W. Irwin

Programming affiliations with the FreeEOS equation-of-state
implementation for stellar interiors (freeeos.sf.net); the Time
Ephemerides project (timeephem.sf.net); PLplot scientific plotting
software package (plplot.org); the libLASi project
(unifont.org/lasi); the Loads of Linux Links project (loll.sf.net);
and the Linux Brochure Project (lbproject.sf.net).
__________________________

Linux-powered Science
__________________________


More information about the CMake mailing list