문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top