difference between #if defined(WIN32) and #ifdef(WIN32)

后端 未结 3 2073
挽巷
挽巷 2020-12-22 16:50

I am compiling my program that will running on linux gcc 4.4.1 C99.

I was just putting my #defines in to separate the code that will be compiled on either windows o

相关标签:
3条回答
  • 2020-12-22 17:34

    If you use #ifdef syntax, remove the brackets.

    The difference between the two is that #ifdef can only use a single condition,
    while #if defined(NAME) can do compound conditionals.

    For example in your case:

    #if defined(WIN32) && !defined(UNIX)
    /* Do windows stuff */
    #elif defined(UNIX) && !defined(WIN32)
    /* Do linux stuff */
    #else
    /* Error, both can't be defined or undefined same time */
    #endif
    
    0 讨论(0)
  • 2020-12-22 17:44
    #ifdef FOO
    

    and

    #if defined(FOO)
    

    are the same,

    but to do several things at once, you can use defined, like

    #if defined(FOO) || defined(BAR)
    
    0 讨论(0)
  • 2020-12-22 17:44

    #ifdef checks whether a macro by that name has been defined, #if evaluates the expression and checks for a true value

    #define FOO 1
    #define BAR 0
    
    #ifdef FOO
    #ifdef BAR
    /* this will be compiled */
    #endif
    #endif
    
    #if BAR
    /* this won't */
    #endif
    
    #if FOO || BAR
    /* this will */
    #endif
    
    0 讨论(0)
提交回复
热议问题