Sheetal T
Sheetal T

Reputation: 88

How to check if CLI defined MACRO (-DMACRO=) is defined or not

How to check if a macro is defined or not, when the definition is provided in compile time.

I've Makefile which resolves and passes this string ("-DMACRO_TEST=${DEFINED}") when compiling sample C file. The variable DEFINED may or may not have a value (0 or non-zero). I wanted to know how to check if the MACRO_TEST is defined or not, when variable DEFINED itself doesn't have any value. This is similar to passing "-DMACRO_TEST=" when compiling the C file.

#include <stdio.h>

#ifdef MACRO_TEST
char msg[] = "MACRO_TEST is defined";
#else
char msg[] = "MACRO_TEST is NOT-defined";
#endif

int main ()
{
    printf("msg = %s\n", msg);
    return 0;
}

Command line output

$ gcc tmp.c -DMACRO_TEST=0
$ ./a.out
msg = MACRO_TEST is defined
$ gcc tmp.c -DMACRO_TEST=
$ ./a.out
msg = MACRO_TEST is defined

when compiling in 2nd case, I was expecting it would return

msg = MACRO_TEST is NOT-defined

Upvotes: 2

Views: 992

Answers (3)

prog-fh
prog-fh

Reputation: 16925

Some common cases for testing macros:

#ifdef MACRO_TEST
  // MACRO_TEST is defined, whatever its content, even empty
  // for example:
  //   -DMACRO_TEST
  //   -DMACRO_TEST=
  //   -DMACRO_TEST=0
  //   -DMACRO_TEST=1
  //   -DMACRO_TEST=abcd
#else
  // MACRO_TEST is not defined, not even with an empty content
#endif

#if MACRO_TEST // it must not be defined as empty (-DMACRO_TEST= --> error)
  // MACRO_TEST has a non-zero integer value
  // for example:
  //   -DMACRO_TEST
  //   -DMACRO_TEST=1    -- equivalent to -DMACRO_TEST
  //   -DMACRO_TEST=23
#else
  // MACRO_TEST has a zero integer value
  // for example:
  //   (not defined at all, then considered as zero)
  //   -DMACRO_TEST=0
  //   -DMACRO_TEST=what
#endif

Upvotes: 2

wildplasser
wildplasser

Reputation: 44250

Just use #if xxx. Undefined xxx is treated as zero by the preprocessor.


#include <stdio.h>

#if MACRO_TEST
char msg[] = "MACRO_TEST is defined";
#else
char msg[] = "MACRO_TEST is NOT-defined";
#endif

int main ()
{
    printf("msg = %s\n", msg);
    return 0;
}

And, you don't have to supply a value on the commandline, just-Dxxx is sufficient, and the compiler/preprocessor will assume 1:

cc -Wall -DMACRO_TEST defined.c

UPDATE. All four cases:


#include <stdio.h>

#if MACRO_TEST
char msg[] = "MACRO_TEST is defined and non-zero";
#else
char msg[] = "MACRO_TEST is NOT-defined or zero";
#endif

int main (int argc, char**argv)
{
    printf("%s: msg = %s\n", argv[0], msg);
    return 0;
}

#!bin/sh

cc -Wall defined.c -o x
cc -Wall -DMACRO_TEST defined.c -o xd
cc -Wall -DMACRO_TEST=0 defined.c -o xd0
cc -Wall -DMACRO_TEST=1 defined.c -o xd1

./x
./xd
./xd0
./xd1

rm x xd xd0 xd1

result:


./x: msg = MACRO_TEST is NOT-defined or zero
./xd: msg = MACRO_TEST is defined and non-zero
./xd0: msg = MACRO_TEST is NOT-defined or zero
./xd1: msg = MACRO_TEST is defined and non-zero

Upvotes: 0

Bodo
Bodo

Reputation: 9875

If you omit any option -DMACRO_TEST or explicitly add -UMACRO_TEST (= undefine macro) in the gcc command line, the macro will not be defined. In this case the preprocessor directive #ifdef MACRO_TEST will be FALSE, and you will get

char msg[] = "MACRO_TEST is NOT-defined";

in the #else branch.

(Assuming you don't define this macro in the code.)

Upvotes: 2

Related Questions