#include files from another directory

Discussion in 'C++' started by Christopher, Jan 15, 2004.

  1. Christopher

    Christopher Guest

    What is the syntax for including files from another directory? In my case I
    want to include a file called "error.h" which lies in a directory called
    "support" which is a directory inside the (project/solution/source or
    whatever you may call it) directory.

    it looks like this (hope the ascii comes out)
    dir engine_x
    .....dir support
    ..............file error.h
    ..............file error.cpp
    .....file main.cpp

    Thanx,
    Christopher
    Christopher, Jan 15, 2004
    #1
    1. Advertising

  2. In article <rnpNb.58466$>, Christopher wrote:
    > What is the syntax for including files from another directory? In my case I
    > want to include a file called "error.h" which lies in a directory called
    > "support" which is a directory inside the (project/solution/source or
    > whatever you may call it) directory.


    Your compiler and/or development environment should allow you to add
    directories to the search path. Specify the header using angle brackets
    <errno.h> and compile with the right flags.

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
    Donovan Rebbechi, Jan 15, 2004
    #2
    1. Advertising

  3. Christopher wrote:
    > What is the syntax for including files from another directory? In my case I
    > want to include a file called "error.h" which lies in a directory called
    > "support" which is a directory inside the (project/solution/source or
    > whatever you may call it) directory.


    #include "support/error.h"

    >
    > it looks like this (hope the ascii comes out)
    > dir engine_x
    > ....dir support
    > .............file error.h
    > .............file error.cpp
    > ....file main.cpp
    >


    The standard way to draw a directory tree is

    engine_x
    |-- support
    | |-- error.h
    | `-- error.cpp
    `-- main.cpp

    Jacques.
    Jacques Labuschagne, Jan 15, 2004
    #3
  4. Christopher

    David Fisher Guest

    "Christopher" <> wrote:

    > What is the syntax for including files from another directory? In my case

    I
    > want to include a file called "error.h" which lies in a directory called
    > "support" which is a directory inside the (project/solution/source or
    > whatever you may call it) directory.
    >
    > it looks like this (hope the ascii comes out)
    > dir engine_x
    > ....dir support
    > .............file error.h
    > .............file error.cpp
    > ....file main.cpp


    There are a few options:

    1. Your C++ compiler probably has a flag to let you specify where to look
    for include files other than the current directory. For gcc the flag is
    "-I"; for Visual C++ 6.0, look in Tools -> Options -> Directories.

    2. You can #include a file in a relative directory, eg. #include
    "engine_x/support/error.h" (assuming the current directory is the one below
    engine_x). Note that all known include directories are searched, so you
    could so something like adding "engine_x" to the include directory list (as
    above) and then say #include "support/error.h"

    3. (Never do this !) Use the absolute path name, eg. #include
    "/home/users/fred/engine_x/support/error.h" This is very bad, because the
    code can never be moved around or given to someone else.

    BTW. If possible, it might be a good idea to rename "error.h" - it is a
    common name, and it might get confusing if the wrong header is included.

    David F
    David Fisher, Jan 15, 2004
    #4
  5. Christopher

    Howard Guest

    "Jacques Labuschagne" <> wrote in message
    news:CRpNb.17268>
    >
    > The standard way to draw a directory tree is
    >
    > engine_x
    > |-- support
    > | |-- error.h
    > | `-- error.cpp
    > `-- main.cpp
    >
    > Jacques.


    Funny...I've looked all through the C++ standard specifications, and
    couldn't find that tree structure anywhere! :)
    Howard, Jan 15, 2004
    #5
  6. Christopher

    Jeff Schwab Guest

    [OT] Re: #include files from another directory

    Jacques Labuschagne wrote:
    > Christopher wrote:
    >
    >> What is the syntax for including files from another directory? In my
    >> case I
    >> want to include a file called "error.h" which lies in a directory called
    >> "support" which is a directory inside the (project/solution/source or
    >> whatever you may call it) directory.

    >
    >
    > #include "support/error.h"
    >
    >>
    >> it looks like this (hope the ascii comes out)
    >> dir engine_x
    >> ....dir support
    >> .............file error.h
    >> .............file error.cpp
    >> ....file main.cpp
    >>

    >
    > The standard way to draw a directory tree is
    >
    > engine_x
    > |-- support
    > | |-- error.h
    > | `-- error.cpp
    > `-- main.cpp
    >
    > Jacques.
    >


    I didn't realize the standard had anything to say on the matter.

    I happen to prefer this:

    engine_x
    |
    +-support
    | |
    | +-error.h
    | +-error.cpp
    |
    +-main.cpp
    Jeff Schwab, Jan 15, 2004
    #6
  7. [OT] Re: #include files from another directory

    Howard wrote:
    > "Jacques Labuschagne" <> wrote in message
    > news:CRpNb.17268>
    >
    >>The standard way to draw a directory tree is
    >>
    >>engine_x
    >>|-- support
    >>| |-- error.h
    >>| `-- error.cpp
    >>`-- main.cpp
    >>
    >>Jacques.

    >
    >
    > Funny...I've looked all through the C++ standard specifications, and
    > couldn't find that tree structure anywhere! :)
    >
    >


    Ahem... Standard according to the Unix 'tree' program :)

    Jacques
    Jacques Labuschagne, Jan 15, 2004
    #7
    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. moon
    Replies:
    3
    Views:
    8,953
    Thomas Fritsch
    Sep 20, 2005
  2. Julien Phalip
    Replies:
    0
    Views:
    492
    Julien Phalip
    Feb 17, 2005
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    885
    Andreas Bogenberger
    Feb 22, 2008
  4. mike
    Replies:
    2
    Views:
    477
    Roedy Green
    Mar 15, 2008
  5. Mike Copeland

    Include Files Directory Structure

    Mike Copeland, Jun 10, 2012, in forum: C++
    Replies:
    7
    Views:
    726
    Balog Pal
    Jun 26, 2012
Loading...

Share This Page