Search code examples
cc-preprocessorconditional-compilation

define only one line in header file


Suppose that there are three lines in a header file such as:

#define line1
#define line2
#define line3

I would like to be sure that line1 should be defined when all of the lines are commented. I also would like to know any two or three lines should not be active (not commented) at the same time. For example if line2 and line3 is active at the same time, compiler should generate an error.

How should i do that?


Solution

  • Make sure line1 is defined when nothing else is:

    #if !defined(line1) && !defined(line2) && !defined(line3)
    #define line1
    #endif
    

    Generate an error if more than one line is defined:

    #if (defined(line1) && defined(line2)) || (defined(line1) && defined(line3)) || (defined(line2) && defined(line3))
    #error "Your message here"
    #endif