Question in including header files (C++)

Started by
7 comments, last by BeerNutts 12 years, 4 months ago
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).

Aluthreney -- the King of sheep.

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
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.
You could also keep your headers in the same directory as your sources and just call

#include "LargeHadronCollider.h"
Rantings, ravings, and occasional insight from your typical code-monkey: http://angryprogrammingprimate.blogspot.com/
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

Stephen M. Webb
Professional Free Software Developer

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?

Aluthreney -- the King of sheep.


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

Stephen M. Webb
Professional Free Software Developer

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

start-cb-include-path.png

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement