Jump to content
• Advertisement

Public Group

# Question in including header files (C++)

This topic is 2566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I've created a header file that I want to include on some source code I've written. In the include I wrote the relative path to the header file, but it gives me an error. I tried writting the complete path to the header file starting with C: and it works fine, but I want to be able to write a relative path because it's more efficient not to mention portable. Is there a way to write a relative path in the #include?

P.S. I'm using codeblocks as my IDE (just saying because my problem could have something to do with the IDE itself).

#### Share this post

##### Share on other sites
Advertisement
Can't really help you without seeing the code that is giving you an error. I use codeblocks as well, and I haven't had a problem with this

#### Share this post

##### Share on other sites
Paths in include directives are relative to the file that included it.

source tree:
/
/stuff
/stuff/source

If a file in /stuff/source includes ../fred.h, then fred.h must appear in /stuff

Also, if /stuff/fred.h needs to include /stuff/john.h, then it must
#include "john.h"

The alternative is to list all your include paths in your makefile/project and then use include <name.h>, but this usually becomes unwieldy after a while.

#### Share this post

##### Share on other sites
You could also keep your headers in the same directory as your sources and just call

#include "LargeHadronCollider.h"

#### Share this post

##### Share on other sites
It is my considerable experience that trying to do something tricky or different will get you in to trouble in the long run. It's all long run.

My recommendation is that your headers all be relative to some recognized top level, preferrably namespaced with your project.

For example, if your project is [font="Courier New"]fred[/font], all of your header files are included with a snippet like this.
#include "fred/fred.h" #include "fred/family/wilma.h"
Then, in your build system, you add an includes search path to your compiler options. I don't know codeblocks, but most compilers will end up with some sort of "-Ipath" argument added to their command line. That could be an absolute or a relative path. You can have several paths to support various configurations (installed devkit, in-tree build, out-of-tree build).

Your source tree could look like this
$(top) +- include -- fred +- fred.h | + family -- wilma.h +-- src -- fred.c or this $(top) +-src +- fred +- fred.h | + family -- wilma.h +-- fred.c
or even something else.

The use of the solidus (for those born this century , "forward slash") as a path separator in the [font="Courier New"]#include[/font] directive will still work on Windows, too.

[color="#808080"]edit: added possible directory layouts

#### Share this post

##### Share on other sites
Well my project is set up like this. I have a folder in my Desktop called Calculator Project. In this folder I have I have my source code (.cpp), the object file (.o) and the program executable (.exe) AND I have a folder named header. In my header folder I have my Calculator.h file.

Now, in my source code I wrote: #include </header/Calculator.h> // It didn't work
Then I wrote: #include <./header/Calculator.h> // It didn't work
Then I wrote: #include <../header/Calculator.h> // It didn't work
Then I wrote #include <C:\Users\Michael\Desktop\Cloth Sack\Calculator Project\header\Calculator.h> // It worked, but this code is not portable which, to me, is the same thing as the other attempts above.

Can anyone tell why I can't make a relative path to my header file?

#### Share this post

##### Share on other sites

... I have a folder named header. In my header folder I have my Calculator.h file.

Now, in my source code I wrote: #include </header/Calculator.h> // It didn't work

Nope. Won't work. Never do that.

Then I wrote: #include <./header/Calculator.h> // It didn't work
[/quote]
Nope. Don't do that. That'll break under a lot of circumstances.

Then I wrote: #include <../header/Calculator.h> // It didn't work
[/quote]
Nope. Don't do that. That'll break under a lot of circumstances.

Then I wrote #include <C:\Users\Michael\Desktop\Cloth Sack\Calculator Project\header\Calculator.h> // It worked, but this code is not portable which, to me, is the same thing as the other attempts above.
[/quote]
That'll work. Sometimes. Never do that. That'll break under a lot of circumstances.

Can anyone tell why I can't make a relative path to my header file?
[/quote]
Your problem is that you're fixing the wrong problem. The problem is that you need to configure your compiler search path in the compiler configuration of your IDE (or whatever tool you're using to configure your compiler), not in the source code you're trying to compile.

Go to your IDE, look for the compiler configuration menu, and look for the bit where you can adjust the search paths for includes.

#### Share this post

##### Share on other sites
Note that #include <file> and #include "file" do two different things. It's technically compiler dependent, but quotation marks generally search the current directory in addition to the include search paths whereas <> only does the include search paths.

#### Share this post

##### Share on other sites
Go to Build Options in CodeBlocks, under there, in Compiler options, there's a second tab for include search directories.

Here's an image (copied from sfml tutorial, http://sfml-dev.org/tutorials/1.6/start-cb.php):

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
• Advertisement

• 9
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013300
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!