Question

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?

Was it helpful?

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

OTHER TIPS

Maybe this is what you want:

#if defined(line2) && defined(line3)
    //generate error
#endif

Add this below the (possibly-commented) definitions of line1, line2 and line3:

#define CHK(a,b,c) (defined(a) && !defined(b) && !defined(c))

#if !(CHK(line1,line2,line3) || CHK(line2,line3,line1) || CHK(line3,line1,line2))
    #error "Improper combination line1/line2/line3"
#endif

This should ensure that exactly one out of three definitions will be active.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top