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

Discussion in 'C++' started by Pablo Suarez, May 18, 2008.

  1. Pablo Suarez

    Pablo Suarez Guest

    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 Suarez, May 18, 2008
    #1
    1. Advertising

  2. Pablo Suarez

    Jim Langston Guest

    Pablo Suarez wrote:
    > 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?

    --
    Jim Langston
     
    Jim Langston, May 18, 2008
    #2
    1. Advertising

  3. Pablo Suarez

    James Kanze Guest

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

    On 18 mai, 04:01, (Pablo Suarez) wrote:
    > 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>


    > instead?


    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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 18, 2008
    #3
  4. Pablo Suarez wrote:
    > 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
     
    Ulrich Eckhardt, May 18, 2008
    #4
  5. Pablo Suarez

    Shen-Ou YE Guest

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

    Pablo Suarez wrote:
    > 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, ...
     
    Shen-Ou YE, May 19, 2008
    #5
  6. Pablo Suarez

    asm23 Guest

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

    Pablo Suarez wrote:
    > 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.
     
    asm23, May 19, 2008
    #6
  7. Pablo Suarez

    James Kanze Guest

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

    On May 19, 2:38 am, Shen-Ou YE <> wrote:
    > Pablo Suarez wrote:
    > > 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?


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

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 19, 2008
    #7
  8. Pablo Suarez

    Fred Guest

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

    On May 17, 7:01 pm, (Pablo Suarez) wrote:
    > 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.
    --
    Fred Kleinschmidt
     
    Fred, May 19, 2008
    #8
  9. Pablo Suarez

    sk_usenet Guest

    "Pablo Suarez" <> wrote in message
    > 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.

    --
    http://techytalk.googlepages.com
     
    sk_usenet, May 19, 2008
    #9
  10. Pablo Suarez

    Gerhard Wolf Guest

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

    Pablo Suarez schrieb:
    > 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 ..)
     
    Gerhard Wolf, May 20, 2008
    #10
  11. Pablo Suarez

    James Kanze Guest

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

    On May 19, 8:36 pm, Fred <> wrote:
    > On May 17, 7:01 pm, (Pablo Suarez) wrote:


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


    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).

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


    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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 20, 2008
    #11
  12. Pablo Suarez

    Lionel B Guest

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

    On Mon, 19 May 2008 15:18:14 -0700, sk_usenet wrote:

    > "Pablo Suarez" <> wrote in message
    >> 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.


    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.

    --
    Lionel B
     
    Lionel B, May 20, 2008
    #12
  13. Pablo Suarez

    Crazy c Guest

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

    On May 17, 9:01 pm, (Pablo Suarez) wrote:
    > 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.
     
    Crazy c, May 20, 2008
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    623
    Jack Klein
    Jul 24, 2004
  2. Replies:
    3
    Views:
    1,329
    Walter Roberson
    May 1, 2006
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    957
    Andreas Bogenberger
    Feb 22, 2008
  4. mlt
    Replies:
    2
    Views:
    880
    Jean-Marc Bourguet
    Jan 31, 2009
  5. Adam Petrie
    Replies:
    8
    Views:
    318
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page