Noeud:Including Directories, Noeud « Next »:, Noeud « Previous »:Verbose Output, Noeud « Up »:Useful GCC Options



Including Directories

gcc searches in the default directories for include files enclosed in #include <...> brackets (the -v flag details which directories are searched in case you do not know). For user defined headers enclosed in quotes, gcc looks in the directory of the current source file being scanned. Thus, the inclusion

#include "func.h"

will search in the current directory for a file named func.h. Simalarly,

#include "../../headers/func.h"

includes a file named func.h two levels up the directory tree in a directory named headers from the current directory of the source file declaring the inclusion. Since this introduces dependance on the way in which files are placed with regard to directory structure, it is better practice to use the -I flag which tells the compiler where to search. In the previous example, compiling with the options

gcc mainFile.c -I../../headers

tells the compiler to look in the ../../headers directory for any files included by the programmer that are not found in the current directory. Specify the -I flag as many times as needed:

gcc mainFile.c -I../../headers -I../../defs -I../../gen

tells the compiler to search two directory levels up in the headers, defs and gen directories.

Using -I will not search for any #include <...> files; use the -I- flag to tell the compiler that any -I commands following the -I- flag should also look for any #include <...>. files.

For example, the command

gcc main.c -I../../headers -I- -I../../defs -I../../gen

will seach for #include "..." files located in the ../../headers directory, and search for #include <...> and #include "..." files located in the ../../defs and ../../gen directories. Note that the current directory will not be searched; to include the current directory, use -I.:

gcc main.c -I../../headers -I- -I../../defs -I../../gen -I.