Reputation: 99428
An Introduction to GCC by Brian Gough says
Incidentally, the difference between the two forms of the include state- ment
#include "FILE.h"
and#include <FILE.h>
is that the former searches forFILE.h
in the current directory before looking in the system header file directories. The include statement#include <FILE.h>
searches the system header files, but does not look in the current directory by default.
But the following example seems to imply the directory containing the source file being compiled, instead of the current directory. What is correct?
$ mv src/hello.h .
$ gcc -c src/main.c
src/main.c:1:10: fatal error: hello.h: No such file or directory
#include "hello.h"
^~~~~~~~~
compilation terminated.
$ mv hello.h src/
$ gcc -c src/main.c
$
Upvotes: 1
Views: 735
Reputation: 140629
https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html states that
By default, the preprocessor looks for header files included by the quote form of the directive
#include "file"
first relative to the directory of the current file, and then in a preconfigured list of standard system directories. For example, if/usr/include/sys/stat.h
contains#include "types.h"
, GCC looks fortypes.h
first in/usr/include/sys
, then in its usual search path.For the angle-bracket form
#include <file>
, the preprocessor’s default behavior is to look only in the standard system directories.
The document you are reading is therefore incorrect. Perhaps Mr. Gough has never attempted to write a nonrecursive Makefile, or separate his source and object directories, and has therefore never noticed that "the current directory" and "the directory containing the current file" are not necessarily the same thing.
GCC has a whole bunch of command line options that you can use to reconfigure how #include
works. There is even an option that turns off looking in the directory of the current file (-I-
), but it is not usable on many operating systems because it will break the C library's headers.
Upvotes: 2