Revision of HOWTO Specify the Header File Include Path for use with MinGW Compilers from 2008, November 26 - 14:47

Printer-friendly version

FIXME: Page under construction; some useful content now, but by no means complete!!!

I will add to this as time permits; please bear with me. My intent is to provide answers to some remarkably similar, but independently asked, questions which have appeared recently, on the MinGW-Users and MinGW-MSYS lists.


Introduction

This Mini-HOWTO attempts to answer the FAQ: "Why can't the MinGW compilers find my project's header files?"

The CPP Section of the GCC Manual indicates that header files may be located in the following directories:--

On a normal Unix system, if you do not instruct it otherwise,
[GCC] will look for headers requested with #include <file> in:

     /usr/local/include
     libdir/gcc/target/version/include
     /usr/target/include
     /usr/include

For C++ programs, it will also look in /usr/include/g++-v3, first.

Of course, the MinGW ports of GCC target Microsoft's Windows system, which is not a "normal UNIX system". Nevertheless, many users, particularly those coming from a UNIX or GNU/Linux background, and especially when they use MSYS to emulate a UNIX file system model on their MS-Windows hosts, are surprised to find that MinGW, using its default configuration, does not automatically search for header files in these directories.

MinGW's Default Include Path

So, if MinGW doesn't search in these "normal" directories, where does it search?

Before answering this, it may be useful to understand why MinGW doesn't search in these directories. Firstly, on a native MS-Windows host, these directories do not typically exist; the multiply rooted nature of the file system means that such path names would be ambiguous. Secondly, since MinGW itself is not a suite of MSYS applications, even if such directories are created in the MSYS environment, MinGW would have no means to locate them.

Thus, to avoid imposing draconian restrictions on how users install MinGW, the maintainers have chosen to adopt a restricted search paradigm, which is determined on the basis of where MinGW is itself installed. The actual search path, for any specific installation of MinGW may be determined, by running the compiler itself, with the "-v" option; typically, for a GCC-3.4.5 installation in c:/mingw:--

$ touch foo.c bar.cpp

$ gcc -v -c foo.c
:
#include "..." search starts here:
#include <...> search starts here:
 c:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include
 c:/mingw/bin/../lib/gcc/mingw32/3.4.5/include
End of search list.
:

$ g++ -v -c bar.cpp
:
#include "..." search starts here:
#include <...> search starts here:
 c:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5
 c:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/mingw32
 c:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward
 c:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include
 c:/mingw/bin/../lib/gcc/mingw32/3.4.5/include
End of search list.
:

This raw format output, as displayed by the compilers, may be difficult for human readers to interpret. Thus, it may be helpful to reduce it to canonical path forms:--

$ gcc -v -c foo.c 2>&1 | sed ':1;s,/[^/.][^/]*/\.\./,/,;t 1'
:
#include "..." search starts here:
#include <...> search starts here:
 c:/mingw/include
 c:/mingw/lib/gcc/mingw32/3.4.5/include
End of search list.
:

$ g++ -v -c bar.cpp 2>&1 | sed ':1;s,/[^/.][^/]*/\.\./,/,;t 1'
:
#include "..." search starts here:
#include <...> search starts here:
 c:/mingw/include/c++/3.4.5
 c:/mingw/include/c++/3.4.5/mingw32
 c:/mingw/include/c++/3.4.5/backward
 c:/mingw/include
 c:/mingw/lib/gcc/mingw32/3.4.5/include
End of search list.
:

Other Directories Searched by Default

The minimal list of directories, identified as described above, specifies the only locations which will be searched by default, for system header files or for headers associated with user installed libraries; however, there is one exception. Astute readers may have noticed that the include file search path is itemised in a pair of sequential lists, with the second being concatenated to the first; however, the first, identified as the #include "..." search list appears to be empty. In reality, this apparent emptiness is possibly misleading; unless the user specifies the "-I-" option when invoking GCC, this list contains exactly one directory: the directory in which the source file containing the #include "file" directive resides.

Notice that this one additional directory is not searched for headers specified with the #include <file> form of the #include directive; it applies only for those headers specified using the #include "file" form of the directive. Also note that the location of this additional directory is not fixed; it is dependent on the location of the source file being compiled, and, while unique for each individual source file, it may vary even within the compilation scope for any project, and even within the scope of a single translation unit, if multiple source files are distributed among different directories in the project's source hierarchy.

Site Status

Site maintenance completed May 25th, 2012 at 12:38 UTC