Search code examples
c++header-filesinclude-path

Equal header file in different include paths


Say we have two header files:/directory1/A.hpp and directory2/A.hpp. The contents of those two headers are different!

Our build target A.cpp needs directory1 and directory2 as include paths, since there are other needed header files in both directories.

Now, in A.cpp there is a #include "A.hpp" statement.

Which version of A.hpp will the preprocessor choose?

Isn't it likely that if one works with a 3rd party software, that so such a situation might occur?


Solution

  • What happens in this case depends on the compiler in question (consult the docs of the compiler you use).

    Situations like these are the reason why using "unqualified" header names is generally a bad idea. Always structure your include directories to leave a part of the path a necessary component of the name. E.g.

    #include <boost/preprocessor.hpp>
    #include <gl/GL.h>
    

    instead of

    #include <preprocessor.hpp>
    #include <GL.h>