0

I found the article Where does Visual Studio look for C++ header files? to be a good start, but I have further questions. The general order that VS looks for include files are (1) local directory, (2) those specified with /I, and (3) those specified by the environment (INCLUDE env var or VC++ settings).

Q1. I think that the /X option turns off (3). Right? Or does it also turn off (1)?

Q2. If I have a nested include file (main.c includes inc1.h, which includes inc2.h), where the first included file is found in one of the /I folders, does VS look for the second included file starting in that same /I folder, or just the local folder of the original source file? VS2008 seems to be operating the first way, but I'd like to find it documented somewhere.

Community
  • 1
  • 1
Jordan
  • 1
  • 1
  • 1

2 Answers2

0

The /X option will not disable looking for files in the local directory (assuming you are including them like "this.h" rather than <this.h>). You can easily test this by creating a file

  #include "foo.h"
  int main() {}

creating an empty foo.h, and compiling using the /X flag.

For Q2, my test with VC2010 showed it behaves the same as VC2008. What I did was had a main:

  #include "inc1.h"
  int main() {}

with a folder inc that contained an inc1.h that just

  #include "inc2.h"

with two different inc2.h files; on in inc and one in the directory with my source file. The one in my source directory was empty, the one in inc had an #error directive. In general, you don't want to rely on this though. Both the C and C++ standards simply say on #include that "the named source file is searched for in an implementation-defined manner".

Jack Lloyd
  • 8,215
  • 2
  • 37
  • 47
  • I found on MSDN some more details about the #include directive. Basically, the 'local directory' means the folder of the file with the #include statement, followed by any file that included it, etc, up to the original source file. http://msdn.microsoft.com/en-us/library/36k2cdd4(v=vs.80).aspx – Jordan Apr 26 '11 at 18:55
0

Q1. The /X option does turn off the INCLUDE var, but not the local directory (see Q2)

Q2. C and C++ compilers, for nested include files, make the location of an include file the current local directory, so you can't turn it off.

eg. If your inc.h file is found in one of the /I folders and it tries to include a file like this:
#include "foo/foo.h" then it must use its own local directory first, before all the other /I folders as they all might have a foo folder with a foo.h file and your inc.h file would probably not compile as its just included the wrong header file.

I've just found this msdn page for VS2008 which seems to have a complete explanation.

quamrana
  • 37,849
  • 12
  • 53
  • 71