tags:

views:

258

answers:

5

In MSVC++ #include files are searched for differently depending on whether the file is enclosed in "" or <>. The quoted form searches first in the local folder, then in /I specified locations, The angle bracket form avoids the local folder.

This means, in MSVC++, its possible to have header files with the same name as runtime and SDK headers.

So, for example, I need to wrap up the windows sdk windows.h file to undefine some macro's that cause trouble. With MSVS I can just add a (optional) windows.h file to my project as long as I include it using the quoted form :-

// some .cpp file
#include "windows.h" // will include my local windows.h file

And in my windows.h, I can pull in the real one using the angle bracket form:

// my windows.h
#include <windows.h> // will load the real one
#undef ConflictingSymbol

Trying this trick with GCC in XCode didn't work. angle bracket #includes in system header files in fact are finding my header files with similar names in my local folder structure. The MSVC system means its quite safe to have a "String.h" header file in my own folder structre. On XCode this seems to be a major no no.

Is there some way to control this search path behaviour in XCode to be more like MSVC's? Or do I just have to avoid naming any of my headers anything that might possibly conflict with a system header. Writing cross platform code and using lots of frameworks means the possibility of incidental conflicts seems large.

A: 

You could try #include "./windows.h". The compiler ought to be smart enough resolve that in the local directory first.

Jon Purdy
A: 

I'm not sure if I understand your question correctly

Avoiding namingproblems is always better ofcourse. the "<>" are more compiler-dependent, while the """" (lol) are more projectspecific.

for example when using your compiler will look into its compiler folders (system) and when using "windows.h" it will look in you projectfolder(s)

if a include "windows.h" is not working, check your makefile or projectsettings

SirLenz0rlot
+2  A: 

You should be able to do the same with gcc (I don't know how much xcode wraps things and prevent access to some features). There is a bunch of options controlling the search path (see http://gcc.gnu.org/onlinedocs/gcc-4.5.0/cpp/Search-Path.html and http://gcc.gnu.org/onlinedocs/gcc-4.5.0/cpp/Invocation.html and look for -iquote). Calling gcc with -v will give you the path specifying where the two differs.

AProgrammer
Reading that document, It seems that GCC has the exact same meaning for <> vs "" that MSVC has - "" means search the parent files directory, THEN the search chains, <> means go straight to the search chains.XCode must be passing a setting to alter this behaviour.
Chris Becke
I never used MSVC, but someone said me that there is a difference: GCC search first the directory of the file containing directly the include, MSVC search the directories of all the files containing directly or indirectly the directive. I don't know of a way to achieve that with GCC.
AProgrammer
BTW, the argument which change that behaviour is -I- (it has two effects: removing the search in the parent file directory and splitting the path in two).
AProgrammer
A: 

Note that gcc supports `-quote

Paul R
A: 

Note that gcc supports -iquote and -isystem in addition to -I to give you more control over which directories will be searched for #includes.

Paul R