#ifdef DEBUG versus #if DEBUG

后端 未结 3 700
闹比i
闹比i 2021-01-30 16:33

It is unclear to me when using compiler directives which of the below two code snippets is correct/preferred and why. It seems that most developers and Open Source projects I\'v

3条回答
  •  后悔当初
    2021-01-30 16:55

    As I know, the finest choice is:

    #ifndef DEBUG
        NSLog(@"-1");
    #elif DEBUG == 0
        NSLog(@"0");
    #else
        NSLog(@"%d", DEBUG);
    #endif
    

    then, you will know #ifndef DEBUG is preferred above all others.

    There is a simpler choice:

    #if DEBUG == 0 // DEBUG is not defined or defined to be 0
        // do sth
    #else
        // do sth
    #endif
    

    However, if -Wundef compiler flag is on, there might be a warning with #if DEBUG == 0.

提交回复
热议问题