How to Include Header Files in Gcc Search Path

How to include header files in GCC search path?

Try gcc -c -I/home/me/development/skia sample.c.

Does `#include FILE.h ` make gcc search for FILE.h in the current directory or somewhere else?

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 for types.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.

How to make g++ search for header files in a specific directory?

A/code.cpp

#include <B/file.hpp>

A/a/code2.cpp

#include <B/file.hpp>

Compile using:

g++ -I /your/source/root /your/source/root/A/code.cpp
g++ -I /your/source/root /your/source/root/A/a/code2.cpp

Edit:

You can use environment variables to change the path g++ looks for header files. From man page:

Some additional environments variables affect the behavior of the
preprocessor.

   CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH

Each variable's value is a list of directories separated by a special character, much like PATH, in which to look for header
files. The special character, "PATH_SEPARATOR", is target-dependent and determined at GCC build time. For Microsoft Windows-based targets it
is a semicolon, and for almost all other targets it is a colon.

CPATH specifies a list of directories to be searched as if specified with -I, but after any paths given with -I options on the
command line. This
environment variable is used regardless of which language is being preprocessed.

The remaining environment variables apply only when preprocessing the particular language indicated. Each specifies a
list of directories to be
searched as if specified with -isystem, but after any paths given with -isystem options on the command line.

In all these variables, an empty element instructs the compiler to search its current working directory. Empty elements can
appear at the beginning
or end of a path. For instance, if the value of CPATH is ":/special/include", that has the same effect as -I.
-I/special/include.

There are many ways you can change an environment variable. On bash prompt you can do this:

$ export CPATH=/your/source/root
$ g++ /your/source/root/A/code.cpp
$ g++ /your/source/root/A/a/code2.cpp

You can of course add this in your Makefile etc.

How to add multiple header include and library directories to the search path in a single gcc command?

Use multiple -I flags for the include directories and multiple -L flags for the lib directories

gcc - how to find path of header include file

gcc has an option -M:

-M Instead of outputting the result of preprocessing, output a
rule suitable for make describing the dependencies of the main source
file. The preprocessor outputs one
make rule containing the object file name for that source file, a colon, and the names of all the included files, including those
coming from -include or -imacros
command line options.

If you do, gcc -M filename.c, it'll list out all headers. Same with g++.

How to tell the gcc to look in the include folder for the header files?

You can use the -I option with gcc to tell the path where to look for the header files.

From online gcc manual

-Idir


Add the directory dir to the head of the list of directories to be searched for header files. This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories. [...]

You can use this option multiple times,

[...] If you use more than one -I option, the directories are scanned in left-to-right order; the standard system directories come after.

Where does gcc look for C and C++ header files?


`gcc -print-prog-name=cc1plus` -v

This command asks gcc which C++ preprocessor it is using, and then asks that preprocessor where it looks for includes.

You will get a reliable answer for your specific setup.

Likewise, for the C preprocessor:

`gcc -print-prog-name=cpp` -v

gcc: local include path hides system header file

Here's what the gcc manual says about include search order:

The lookup order is as follows:

  1. For the quote form of the include directive, the directory of the current file is searched first.
  2. For the quote form of the include directive, the directories specified by -iquote options are searched in left-to-right order, as
    they appear on the command line.
  3. Directories specified with -I options are scanned in left-to-right order.
  4. Directories specified with -isystem options are scanned in left-to-right order.
  5. Standard system directories are scanned.
  6. Directories specified with -idirafter options are scanned in left-to-right order.

As you can see, files inlcuded by the -I option (3) are searched before standard system directories (5) (which includes /usr/include).

So in general, files in directories you add with -I will hide system headers of the same name, regardless of whether those are included with <> or "".

The difference between "" and <> is that files included with "" first search the current directory (and the -iquote directories, but that is much more obscure). So if you include a file like #include "foo/bar.h" and this file includes "endian.h", then if foo/endian.h exists it will be used rather than a file by the same name from the standard include directories. However, if you #include <endian.h> the system version will be used. In that way, unintended name collisions are reduced in both direction if you use the correct quoting.



Related Topics



Leave a reply



Submit