Question in including header files (C++)
#1 Members - Reputation: 207
Posted 01 December 2011 - 05:09 PM
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.
#3 Members - Reputation: 100
Posted 02 December 2011 - 05:12 AM
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 Members - Reputation: 107
Posted 02 December 2011 - 07:25 AM
#include "LargeHadronCollider.h"
#5 Members - Reputation: 2777
Posted 02 December 2011 - 08:04 AM
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.cor this$(top) +-src +- fred +- fred.h
| + family -- wilma.h
+-- fred.cor 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
Professional Free Software Developer
#6 Members - Reputation: 207
Posted 02 December 2011 - 09:19 AM
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 Members - Reputation: 2777
Posted 02 December 2011 - 10:01 AM
Nope. Won't work. Never do that.... 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. 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 <../header/Calculator.h> // It didn't work
That'll work. Sometimes. Never 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.
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.Can anyone tell why I can't make a relative path to my header file?
Go to your IDE, look for the compiler configuration menu, and look for the bit where you can adjust the search paths for includes.
Professional Free Software Developer
#8 Moderators - Reputation: 6672
Posted 02 December 2011 - 11:01 AM
#9 Members - Reputation: 1566
Posted 02 December 2011 - 05:08 PM
Here's an image (copied from sfml tutorial, http://sfml-dev.org/tutorials/1.6/start-cb.php):

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






