简体   繁体   中英

Using macros with the same name in different header files

I use macros like #DEBUG to print some additional debugging info and even possibly do something differently to help me with debugging. For example:

in header ah:

#define DEBUG 1

in src ac:

#include "a.h"

int func_a () {
/*some code*/
#if DEBUG
//do this
#endif
}

What will happen if I use a macro with the same name in another file ?

header bh

#define DEBUG 1
#if DEBUG
    # define PRINT 1
#elif
    #define PRINT 0
#endif

src bc

#include "a.h"
#include "b.h"

int func_b () {
/*some code*/
#if PRINT
//do this
#endif
/*some code*/
#if DEBUG
//do this
#endif
}

What will happen if I change the value of #DEBUG in one of the headers? I saw in some other answers that redefining a macro is not allowed in the C standard. But when I compile with GCC using the -Wall flag I see no errors or warnings.

What will happen if I use a macro with the same name in another file ?

It depends. C does not allow an identifier that is already defined as a macro name at some point in a translation unit to be defined again at that point, unless the redefinition specifies an identical replacement list. This is a language constraint, so conforming implementations will emit a diagnostic about violations they perceive. Compilers may reject code that contains violations, and if they nevertheless accept such code then the resulting behavior is undefined as far as C is concerned.

In practice, implementations that do accept such violations have two reasonable choices (and a universe of unreasonable ones):

  • ignore the redefinition, or
  • process the redefinition as if it were proceeded by an #undefine directive specifying the affected macro name.

Implementations of which I am aware accept such redefinitions and implement the latter option, at least by default.

If your headers are defining macros solely for their own internal use then you may be able to address the issue by exercising some discipline:

  1. Each header puts all its #include directives at the beginning, before any definition of the possibly-conflicting macro(s).
  2. Each header #undefine s the possibly-conflicting macro at the end, under all conditional-compilation scenarios in which the macro may be defined in the first place.

On the other hand, if the macro is intended to be referenced by files that use the header(s) where it is defined then undefining it within the header would defeat the purpose. Under some circumstances, probably including yours, you can address that by defining the macro only conditionally in each header:

#if !defined(DEBUG)
#define DEBUG 1
#endif

That will avoid redefinition, instead using (only) the first definition encountered, which may even come from compiler command-line arguments. If you do this, however, it is essential that all the default definitions specified in that way be the same, else changing your headers' inclusion order will have unexpected effects code that depends on which definition is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM