[CMake] option, configure_file and ON/OFF

Michael Hertling mhertling at online.de
Tue Nov 23 19:02:55 EST 2010


On 11/23/2010 10:30 PM, Anton Deguet wrote:
> Hello,
> 
> I am trying to use a UI option to set a value used in configure_file and ideally obtain a file that doesn't use ON/OFF.   Here is a CMake sample:
>   option (MYLIB_HAS_WHATEVER "Use whatever library" OFF)
> 
> In my config.h.in I have:
>   #define ON 1
>   #define OFF 0
>   #define MYLIB_HAS_WHATEVER ${MYLIB_HAS_WHATEVER}
> 
> Then in my code I can use:
>   #if MYLIB_HAS_WHATEVER
>   ....
>   #endif
> 
> It worked so far but there is an issue if we end up using other libraries which also define ON and/or OFF.  There seems to be no way to tell CMake to use 1/0 instead of ON/OFF during the configure_file.  My solution has been to modify my CMake to do the following:
>   option (MYLIB_HAS_WHATEVER "Use whatever library" OFF)
>   if (MYLIB_HAS_WHATEVER)
>     set(MYLIB_CONFIG_HAS_WHATEVER 1)
>     ....
>   else (MYLIB_HAS_WHATEVER)
>     set(MYLIB_CONFIG_HAS_WHATEVER 0)
>     ....
>   endif (MYLIB_HAS_WHATEVER)
> 
> and in the config.h.in do:
>   #define MYLIB_HAS_WHATEVER ${MYLIB_CONFIG_HAS_WHATEVER}
> 
> Is there a better way?

In your config.h.in, simply write:

#cmakedefine01 MYLIB_HAS_WHATEVER

With CONFIGURE_FILE(), this expands to

#define MYLIB_HAS_WHATEVER 0

or

#define MYLIB_HAS_WHATEVER 1

depending on the value of MYLIB_HAS_WHATEVER.

Regards,

Michael


More information about the CMake mailing list