#include files from another directory

C

Christopher

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
 
D

Donovan Rebbechi

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,
 
J

Jacques Labuschagne

Christopher said:
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.
 
D

David Fisher

Christopher said:
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
 
H

Howard

Jacques Labuschagne said:
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! :)
 
J

Jeff Schwab

Jacques said:
#include "support/error.h"


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
 
J

Jacques Labuschagne

Howard said:
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
 

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,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top