11

Based on this question How to catch empty defined macro with gcc? I have another problem. How to catch undefined macro in preprocessor #if condition? Example code:

#include <stdio.h> 

int main()
{
#if ENABLE_SOMETHING == 1
    ... do something ..
#endif
 return 0;
}

Is there a way to catch error/warning when ENABLE_SOMETHING is not set using gcc compiler(maybe some flag)? Or maybe there are external tools which I can use?


I know than i can write something like this :

#ifndef ENABLE_SOMETHING
    #error "ENABLE_SOMETHING undefined!"
#endif

But I have a huge amount of different defines(ENABLE_STH1, ENABLE_STH2, ENALBE_STH3 ... etc.) in code and i don't want to fix this manually. I'm looking for some automatic solution for our project.

11
  • You will get a pre-processor error if macro is not defines in above example. Something like this error: 'MAX_N_LENGTH' undeclared Commented Feb 1, 2019 at 9:27
  • If MAX_N_LENGTH is undefined then the compiler will not compile the code. Commented Feb 1, 2019 at 9:27
  • Ok, my mistake, wrong example. Wait a moment. Commented Feb 1, 2019 at 9:29
  • Are you saying that you have undefined tokens used in your code, and it's compiling? I don't understand this question. Commented Feb 1, 2019 at 9:38
  • Yes, I use these tokens to enable/disable some parts of code in diffrent build configurations. But i want to have some guard for future errors if somebody will make new #if but will forgot to add #define (which happend). Commented Feb 1, 2019 at 9:40

4 Answers 4

11

Is there a way to catch error/warning when ENABLE_SOMETHING is not set using gcc compiler(maybe some flag)?

With GCC you can use the -Wundef flag (clang supports it as well).

From the official documentation

-Wundef

Warn if an undefined identifier is evaluated in an #if directive. Such identifiers are replaced with zero.

EDIT:

For example, this C-code:

#include <stdio.h>

int main(void)
{
#if UNDEFINED_MACRO
  printf("hi mum!\n");
#endif

  return 0;
}

... compiled with GCC and the -Wundef flag yields this:

$ gcc undef.c -Wundef
undef.c: In function 'main':
undef.c:5:5: warning: "UNDEFINED_MACRO" is not defined [-Wundef]
 #if UNDEFINED_MACRO
     ^
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, so -Wundef together with -Werror will give me an error, won't it?
@s.paszko: Yes. -Werror converts warnings into errors that makes compilation fail, so that would emit an error if the compiler encountered an undefined macro
0

Let's assume you have this code and it compiles, but you don't know if MAX_N_LENGTH is a macro, or if it's something else:

int main()
{
    int a = MAX_N_LENGTH; // MAX_N_LENGTH could also be an int declared somewhere else
    return 0;
}

You can check whether it actually is a macro like this:

#ifdef MAX_N_LENGTH
    printf("MAX_N_LENGTH is a macro.\n");
#else
    printf("MAX_N_LENGTH is NOT macro.\n");
#endif // MAX_N_LENGTH

Of course, if that ever is an issue, I'd rethink my naming conventions.

3 Comments

Thanks for question but i've gave wrong example. Question redited.
The same still applies, you can use #ifdef ... #endif to get conditional compilation depending on whether the macro in question exists or not.
Ok, added info in question. I know this solution but i'm looking for some automatic solution.
0

You could try something like the following:

#ifndef MAX_N_LENGTH
#warning "MAX_N_LENGTH is undefined"
    int array[16];
#else
    int array[MAX_N_LENGTH + 1];
#endif

1 Comment

Thanks for question but i've gave wrong example. Question redited.
0

You can test if a macro is defined in a #if preprocessor expression with defined(ENABLE_SOMETHING):

#if !defined(ENABLE_SOMETHING)
  #error ENABLE_SOMETHING is not defined
#endif

You can handle macros with an empty definition this way:

#if ENABLE_SOMETHING + 0 == 1
   /* ENABLE_SOMETHING is defined and not 0 or empty */
#endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.