Sign in to follow this  
Aluthreney

Question in including header files (C++)

Recommended Posts

Aluthreney    275
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


Link to post
Share on other sites
Soundman32    100
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


Link to post
Share on other sites
3kimr    107
You could also keep your headers in the same directory as your sources and just call

[code]#include "LargeHadronCollider.h"[/code]

Share this post


Link to post
Share on other sites
Bregma    9202
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 [b][font="Courier New"]fred[/font][/b], all of your header files are included with a snippet like this.
[code]#include "fred/fred.h"
#include "fred/family/wilma.h"[/code]
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
[code]$(top) +- include -- fred +- fred.h
| + family -- wilma.h
+-- src -- fred.c[/code]
or this
[code]$(top) +-src +- fred +- fred.h
| + family -- wilma.h
+-- fred.c[/code]
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"][i][size="2"]edit: added possible directory layouts[/size][/i][/color]

Share this post


Link to post
Share on other sites
Aluthreney    275
Well my project is set up like this. I have a folder in my Desktop called [b]Calculator Project[/b]. 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 [b]header[/b]. In my [b]header folder[/b] I have my [b]Calculator.h file[/b].

Now, in my source code I wrote: [b]#include </header/Calculator.h>[/b] // It didn't work
Then I wrote: [b]#include <./header/Calculator.h>[/b] // It didn't work
Then I wrote: [b]#include <../header/Calculator.h>[/b] // It didn't work
Then I wrote [b]#include <C:\Users\Michael\Desktop\Cloth Sack\Calculator Project\header\Calculator.h>[/b] // 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


Link to post
Share on other sites
Bregma    9202
[quote name='Aluthreney' timestamp='1322839164' post='4889790']
... I have a folder named [b]header[/b]. In my [b]header folder[/b] I have my [b]Calculator.h file[/b].

Now, in my source code I wrote: [b]#include </header/Calculator.h>[/b] // It didn't work
[/quote]
Nope. Won't work. Never do that.
[quote]
Then I wrote: [b]#include <./header/Calculator.h>[/b] // It didn't work
[/quote]
Nope. Don't do that. That'll break under a lot of circumstances.
[quote]
Then I wrote: [b]#include <../header/Calculator.h>[/b] // It didn't work
[/quote]
Nope. Don't do that. That'll break under a lot of circumstances.
[quote]
Then I wrote [b]#include <C:\Users\Michael\Desktop\Cloth Sack\Calculator Project\header\Calculator.h>[/b] // 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.
[quote]
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


Link to post
Share on other sites
SiCrane    11839
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


Link to post
Share on other sites
BeerNutts    4400
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, [url="http://sfml-dev.org/tutorials/1.6/start-cb.php"]http://sfml-dev.org/tutorials/1.6/start-cb.php[/url]):

[img]http://sfml-dev.org/tutorials/1.6/images/start-cb-include-path.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this