[CMake] #cmakedefine vs #define

Alan W. Irwin irwin at beluga.phys.uvic.ca
Tue Mar 15 21:30:33 EDT 2011


On 2011-03-15 16:26-0600 Balamurali Ananthan wrote:

> Hello,
>
> Sorry if this question is too primitive. I am trying to extract the build dir 
> into the source code by populating it in a variable in my config.h file.
>
> I do this by adding the following line in config.h.in
>
> #cmakedefine PROJ_BUILD_DIR_CMAKEDEFINE "${PROJECT_BINARY_DIR}"
>
> In the config.h, this produces
>
> /* #undef PROJ_BUILD_DIR_CMAKEDEFINE */
>
>
>
> But if i replace it to this in config.h.in,
>
> #define PROJ_BUILD_DIR_DEFINE "${PROJECT_BINARY_DIR}"
>
> Then the right value is populated in the config.h
>
> #define PROJ_BUILD_DIR_DEFINE "/home/bala/projects/myproj/trunk/builds"
>
> Wondering why didn't the cmakedefine work in this case? Any clues?

The man page for cmake covers this.  "#cmakedefine VAR will be
replaced with either #define VAR or /* #undef VAR  */
depending on  the  setting  of  VAR in CMake."

Note that VAR is the CMake variable name and not its value.
PROJ_BUILD_DIR_CMAKEDEFINE does not exist as a CMake variable in your
project so its CMake value is false and you get the above commented
out #undef result just like the man page says.  OTOH if you had
specified instead,

#cmakedefine PROJECT_BINARY_DIR

that would have configured (since PROJECT_BINARY_DIR is "True" in the
CMake sense for your project) as

#define PROJECT_BINARY_DIR

Of course, that would not have been too helpful to you since there is
no associated explicit value, but note #cmakedefine is quite useful
for the case of conditional programming depending on #ifdef (i.e.,
True/False binary macro logic).

For cases where you need to #define an explicit value, then you should
forget about #cmakedefine and use the configured #define (as you did
above).

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.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