Olumide
Olumide

Reputation: 5829

Missing binary operator before token "#" error when #define is inside #if

I'm trying to define a macro GCC_447_OR_LESS (below) that I can use to check for instead of using the great a big ugly macro

#ifdef __linux__
    // Test for GCC 4.4.7 or less
    #if __GNUC__ < 4 ||                                                         \
        __GNUC__ == 4 && ( __GNUC_MINOR__ < 4 ||                                \
                         ( __GNUC_MINOR__ == 4 && __GNUC_PATCHLEVEL__ <= 7 ) )  \
        #define GCC_447_OR_LESS
    #endif
#endif

However I'm getting the error

error: missing binary operator before token "#"
 #define GCC_447_OR_LESS
 ^

I can't explain what's going on. Can't a #define be used within #if in the way I used it?

Upvotes: 0

Views: 1423

Answers (1)

Raxvan
Raxvan

Reputation: 6515

The character \ tells the preprocessor that the current line is continued on the next. In your case the last if line ( __GNUC_MINOR__ == 4 && __GNUC_PATCHLEVEL__ <= 7 ) ) \ adds the define to the #if condition. The #define is part of the true block and must not be included in the #if line

Fixed code is:

#ifdef __linux__
    // Test for GCC 4.4.7 or less
    #if __GNUC__ < 4 ||                                                         \
        __GNUC__ == 4 && ( __GNUC_MINOR__ < 4 ||                                \
                         ( __GNUC_MINOR__ == 4 && __GNUC_PATCHLEVEL__ <= 7 ) ) /**/
        #define GCC_447_OR_LESS
    #endif
#endif 

Upvotes: 3

Related Questions