Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C preprocessor redefine conflict dependent on include order

I just had a redefine conflict in the project I'm working on and while tracing down why it's not happening on all platforms (turned out to be to order of includes), I stumbled upon the following behavior which I cannot explain.

1. compiles without warnings

    #define LIST_HEAD(a) { int a = 0; }                                                                                                                     
    #include <sys/queue.h>                                                          

    int main() {                                                                    
        return 0;                                                               
    }

2. "macro redefined" warning

    #include <sys/queue.h>
    #define LIST_HEAD(a) { int a = 0; }                                                                                                       

    int main() {                                                                    
        return 0;                                                               
    }

I would expect both cases to produce the warning, since there're no checks in <sys/queue.h> that would prevent a redefine.

So why does the first case produces no warning, while the second one does? What I'm missing here?

Btw: I get the same results on my Mac with clang and my Linux box with gcc.

like image 526
grasbueschel Avatar asked Dec 16 '12 16:12

grasbueschel


1 Answers

By default, this warning is suppressed in system headers. The code in <sys/queue.h> is considered to come from a system header because sys/queue.h was found by searching a path marked as containing system headers.

So in (2) you see the warning because it is generated within your code, while in (1) the warning is generated within queue.h, and so is suppressed. Add -Wsystem-headers to your compilation options, and you'll see the warning in both cases.

like image 190
John Marshall Avatar answered Nov 10 '22 10:11

John Marshall