Two different CMake difinitions

63 Views Asked by At

With CMake when some definitions are defined they are defined in this way:

add_definitions(-DMY_DEFINITION)

Sometimes I see people make the definitions in a different way:

add_definitions(-DMY_DEFINITION=1)

Then my question is what's the difference between them in the generated C++ project. Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

This is not really related to CMake but more to the C/++ compiler. In the code the difference is the same between :

#define MY_DEFINITION

and

#define MY_DEFINITION 1

Actually there's not need to define a value for a C/++ macro if the only thing you want is to know if the macro exists (has been defined), like a "flag". Best example is the header include guards :

#ifndef MYHEADER
#define MYHEADER
// ...
#endif

But sometimes people prefer setting a value (like =1) even if they don't need it, because it's more exhaustive, or clear.

More generally speaking when you affect a value to a macro it is because you expect the macro name to be expanded to the value. When not you just expect the value to exist. The way tests are done may be different :

With -DMY_DEFINITION:

#ifdef MY_DEFINITION
    // Do something
#else
    // Do somthing else
#endif

With -DMY_DEFINITION=1

#if MY_DEFINITION
    // Do something
#else
    // Do somthing else
#endif