How to determine the directories from where "#include <.....>" gets the header files?

P

Pablo Suarez

When I code

#include "myheader.h"

then this header file is searched in the current directory.

But where does the compiler search the header file when I write

#include <myheader.h>

instead?

Pablo
 
J

Jim Langston

Pablo said:
When I code

#include "myheader.h"

then this header file is searched in the current directory.

But where does the compiler search the header file when I write

#include <myheader.h>

instead?

Depends on the compiler. However the compiler is set up to search for them.
Standard library header directories, etc...

Are you asking what the standard says about this, how to determine for your
specific compiler or something else?
 
J

James Kanze

When I code
#include "myheader.h"
then this header file is searched in the current directory.

Not with the compilers I use. The de facto standard is to first
look for it in the directory which contained the file containing
the header declaration.
But where does the compiler search the header file when I write
#include <myheader.h>

Where ever it wants to. In theory, at least, there may not even
be a file. (As far as the standard is concerned, you can only
use this form for the standard headers.)

According to the standard, the compiler has two different rules,
one for looking up "...", and another for looking up <...>, and
the second need not even be a file; the compiler could "know"
the contents of all of the standard headers, and simply
incorporate that knowledge directly. In addition, the standard
says that if lookup for "..." fails, the compiler must attempt
again as if it were <...>.

In practice, the rules are:

-- if the "header.hh" form is used, look in the directory
containing the file with the include, then

-- look in each of the directories specified by the -I or /I
options, in the order the options appeared, and finally

-- look in a number of predefined locations---under Unix, this
is typically some locations depending on where the compiler
is installed, plus /usr/include.

In practice, too, that's just a minimal set. A lot of compilers
have additional options or rules.
 
U

Ulrich Eckhardt

Pablo said:
When I code

#include "myheader.h"

then this header file is searched in the current directory.

But where does the compiler search the header file when I write

#include <myheader.h>

instead?

There are two answers to this, one as given by the C and C++ language
standards and one that applies in reality.

1. The language defines several so-called headers like <stdio.h> (C) or
<iostream> (C++). These are not ever required to be files at all, it is
only required to have certain effects if you include them. In reality, most
compilers install these headers as part of the compiler installation.

2. In practice, you will find in the same dir as other compiler-supplied
headers are installed in also some files that are system-dependent.
Further, often libraries install their header files in that dir or add a
directory with their headers to the path searched for system include files.

Suggestion: if file X includes file Y, use #include "Y" if Y is part of the
same program or library as X. If Y belongs to a separate library, use
#include <Y> and if necessary adjust the compiler settings to include that
library.

Uli
 
S

Shen-Ou YE

Pablo said:
When I code

#include "myheader.h"

then this header file is searched in the current directory.

But where does the compiler search the header file when I write

#include <myheader.h>

instead?

Pablo

Hi,

When you write :

#include <myheader.h>

myheader.h is searched in the default system include directories. Under
Unix, it's typically /usr/include, /usr/X11/include, ...
 
A

asm23

Pablo said:
When I code

#include "myheader.h"

then this header file is searched in the current directory.

But where does the compiler search the header file when I write

#include <myheader.h>

instead?

Pablo
hi, each compiler will put the standard header files in some directory
,eg, I'm using Visual c++, the directory is something like Microsoft
Visual Studio\VC98\INCLUDE...
so, depend on the compiler you use, you can find them. Also, with -I,
you can specifies an additional directory to search for include files.
 
J

James Kanze

When you write :
#include <myheader.h>
myheader.h is searched in the default system include
directories. Under Unix, it's typically /usr/include,
/usr/X11/include, ...

Not only. First, any directories specified by -I are searched,
then some compiler specific directories
("/opt/SUNWspro/include", for example, or
"~/gnu/gcc/install-4.1.0/include"), and finally the default
system directories (usually just "/usr/include"). Of course,
the first form of the include will search all these as well, if
it doesn't find the file in the directory where the source file
including it is located.
 
F

Fred

When I code

#include "myheader.h"

then this header file is searched in the current directory.

Almost. It is *first* looked for in the current directory.
If not found, it *may* be looked for in other places,
according to implementation-dependent rules.
But where does the compiler search the header file when I write

#include <myheader.h>

instead?

It is looked for in implementation-defined "standard" directories.
ON *nix, this is usually /usr/include.

Also, most compilers allow command-line options to specify
additional directories to be searched BEFORE the standard place(s),
using the -I option:
cc -I/MyDirectory -I/MyOtherDirectory ...

These directories will be searched for with either of the #include
styles.
 
S

sk_usenet

Pablo Suarez said:
When I code

#include "myheader.h"

then this header file is searched in the current directory.

But where does the compiler search the header file when I write

#include <myheader.h>

instead?

You specify it to the compiler. Check your compiler's documentation as to
how you control it so search directories for header files. With gcc the
option is -I.
 
G

Gerhard Wolf

Pablo said:
When I code

#include "myheader.h"

then this header file is searched in the current directory.

But where does the compiler search the header file when I write

#include <myheader.h>

instead?

Pablo
INCLUDE Environment variable or compiler searchpath (-I ..)
 
J

James Kanze

On May 17, 7:01 pm, (e-mail address removed) (Pablo Suarez) wrote:
Almost. It is *first* looked for in the current directory.
If not found, it *may* be looked for in other places,
according to implementation-dependent rules.

It's very much compiler dependent, but I don't know of any which
look in the current directory (unless you specifically tell the
compiler to do so).
It is looked for in implementation-defined "standard" directories.
ON *nix, this is usually /usr/include.

Again, it's very implementation dependent, and almost always
includes more than just /usr/include.
Also, most compilers allow command-line options to specify
additional directories to be searched BEFORE the standard place(s),
using the -I option:
cc -I/MyDirectory -I/MyOtherDirectory ...
These directories will be searched for with either of the
#include styles.

The rules say that for an "..." include, if the search fails,
the compiler must then treat it as a <...> include.
 
L

Lionel B

You specify it to the compiler. Check your compiler's documentation as
to how you control it so search directories for header files. With gcc
the option is -I.

Further to this: if you want to know where the current invocation of g++
is searching for headers (system and other), specify the -v option on the
g++ command.
 
C

Crazy c

When I code

#include "myheader.h"

then this header file is searched in the current directory.

But where does the compiler search the header file when I write

#include <myheader.h>

instead?

Pablo

Pablo,

I belive, if I'm not mistaken, it searches the current directory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top