Jump to content

  • Log In with Google      Sign In   
  • Create Account

Question in including header files (C++)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Aluthreney   Members   -  Reputation: 256

Like
0Likes
Like

Posted 01 December 2011 - 05:09 PM

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.


Sponsor:

#2 nitishk   Members   -  Reputation: 161

Like
0Likes
Like

Posted 01 December 2011 - 09:30 PM

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

#3 soundman32   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 December 2011 - 05:12 AM

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.

#4 ekimr   Members   -  Reputation: 107

Like
0Likes
Like

Posted 02 December 2011 - 07:25 AM

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/

#5 Bregma   Crossbones+   -  Reputation: 5504

Like
0Likes
Like

Posted 02 December 2011 - 08:04 AM

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 fred, 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 #include directive will still work on Windows, too.

edit: added possible directory layouts
Stephen M. Webb
Professional Free Software Developer

#6 Aluthreney   Members   -  Reputation: 256

Like
0Likes
Like

Posted 02 December 2011 - 09:19 AM

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.


#7 Bregma   Crossbones+   -  Reputation: 5504

Like
0Likes
Like

Posted 02 December 2011 - 10:01 AM

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

Nope. Don't do that. That'll break under a lot of circumstances.

Then I wrote: #include <../header/Calculator.h> // It didn't work

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.

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?

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

#8 SiCrane   Moderators   -  Reputation: 9676

Like
0Likes
Like

Posted 02 December 2011 - 11:01 AM

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.

#9 BeerNutts   Crossbones+   -  Reputation: 3018

Like
0Likes
Like

Posted 02 December 2011 - 05:08 PM

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

Posted Image

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)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS