[CMake] #cmakedefine and the #if vs #ifdef argument

Pau Garcia i Quiles pgquiles at elpauer.org
Thu Jun 11 17:47:57 EDT 2009


On Thu, Jun 11, 2009 at 11:32 PM, Hostile Fork<hostilefork at gmail.com> wrote:
> Hello list!
>
> As a learning exercise, I am adding CMake and CTest to a small open-source
> library I made which currently has no build system:
>
>        http://hostilefork.com/nstate/
>        http://hostilefork.com/nocycle/
>
> For the first step, I have been applying the "configure_file" methodology to
> this header:
>
>
>  http://github.com/hostilefork/nocycle/blob/1ac238aea7af9e02f3a49f0c7eb991074c8eb3fd/NocycleSettings.hpp
>
> ( Following these directions:
> http://www.vtk.org/Wiki/CMake_HowToDoPlatformChecks )
>
> It seems the #cmakedefine lines are replaced with one of these two cases:
>
>        #define VAR_THAT_IS_ON
>        /* #undef VAR_THAT_IS_OFF */
>
> However... in the past I have been persuaded by the argument that the use of
> #if is superior to #ifdef for conditional compilation.  ( Roddy's comment
> here on StackOverflow summarizes the advantages pretty well:
> http://stackoverflow.com/questions/135069/ifdef-vs-if-which-is-bettersafer )
>
> Is it possible to get CMake to produce something more like:
>
>        #define VAR_THAT_IS_ON 1
>        #define VAR_THAT_IS_OFF 0
>
> There are other questions I have lined up.  :)  But I'll start with just
> that one, and if anyone wants to be proactive offer advice/pointers/urls
> based on the existing source... please jump in!

You can do something like in your headerfile.h.cmake :

#define VAR_THAT_IS_ON @VAR_THAT_IS_ON@
#define VAR_THAT_IS_OFF @VAR_THAT_IS_OFF@

If you call CMake with 'cmake -DVAR_THAT_IS_ON=1 -DVAR_THAT_IS_OFF=0',
it will produce:

#define VAR_THAT_IS_ON 1
#define VAR_THAT_IS_OFF 0

Problem is, if CMake is invoked with anything else (for instance
'cmake -DVAR_THAT_IS_ON=foo -DVAR_THAT_IS_OFF=bar' ), it will produce:

#define VAR_THAT_IS_ON foo
#define VAR_THAT_IS_OFF bar

You'd need to do something more complex (using IF-THEN-ELSE) in CMake
to make sure only '1' and '0' are the values used for the defines.

-- 
Pau Garcia i Quiles
http://www.elpauer.org
(Due to my workload, I may need 10 days to answer)


More information about the CMake mailing list