Microsoft KB Archive/38383

From BetaArchive Wiki

INFO: Include Directories Not Searched as Expected

Article ID: Q38383

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft C for MS-DOS, versions 6.0, 6.0a, 6.0ax - Microsoft C for OS/2, versions 6.0, 6.0a - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 4.0, 5.0

SUMMARY

It may appear that the C compiler searches the INCLUDE list in the wrong order. While the search order for a file enclosed in brackets (<>) is simple, the order for a file enclosed in double quotation marks ("") is different and more complicated.

MORE INFORMATION

NOTE: Using double quotation marks around a complete path specification causes the standard directories to NOT be searched.

As documented in the "Include Files" section of the Visual C++, 16-bit edition, "Microsoft C Language Reference," and "The #include Directive" section of the Visual C++, 32-bit edition, "Preprocessor Reference," the search order for a statement like "#include <file.h>" is as follows:

1. Any directories specified using the /I switch on the CL command

   line, from left to right.

2. Directories specified in the include environment variable, again

   from left to right.

3. If the file is not found in either of these steps, the following error

   is issued:

      fatal error C1015:  cannot open include file 'file.h'

   The Microsoft C/C++ compiler included with Visual C++, 32-bit edition,
   will issue the following error:

      fatal error C1083: Cannot open include file: 'file.h': No such file
   or directory

For the following example, only the "\path" directory on the current default drive is searched:

   #include "\path\file.h"

The standard directories are not searched. However, the search order for

   #include "file.h"

is similar to the search order for

   #include <file.h>

except that "parent directories" are searched before directories specified by the /I switch and before directories specified in the INCLUDE environment variable. The parent directory is the directory that contains the source containing the #include directive. If #include files are nested, then the parent directories are searched in reverse order of the nesting: first parents, then grandparents, and so on.

For example, if source file GRANDMA.C contains

   #include <parent.h>

and PARENT.H contains

   #include "child.h"

the search for CHILD.H will take place in the following order: 1. The parent directory--in this case, the directory in which PARENT.H

   was previously found.

2. If CHILD.H is not there, the directory that contains GRANDMA.C

   will be searched next.

3. If CHILD.H is still not found, the directories (if any) specified

   in /I switches on the CL command line will be searched in left-to-
   right order.

4. If CHILD.H is still not found, the directories (if any) specified

   by the INCLUDE environment variable will be searched in left-to-
   right order.

5. If CHILD.H was not found in any of these places, the compiler will issue

   the following error:

      fatal error C1015:  cannot open include file 'child.h'

   The Microsoft C/C++ compiler included with Visual C++, 32-bit edition,
   will issue the following error:

      fatal error C1083: Cannot open include file: 'child.h': No such file
   or directory

Additional query words: 8.00 8.00c 9.00

Keywords          : kbCompiler 
Version           : MS-DOS:6.0,6.00a,6.00ax,7.0; OS/2:6.0,6.00a;  WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,4.0,5.0
Platform          : MS-DOS NT OS/2 WINDOWS
Issue type        : kbinfo

Last Reviewed: October 3, 1997
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.