Thread: How do I include a file in a child directory of a parent directory of the current dir

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    How do I include a file in a child directory of a parent directory of the current dir

    What is the correct syntax in C++ to include a header in a child directory of a parent directory of the current directory?

    Is it this:

    #include "/../../example_header.h/headers"

    or is it this:

    #include "/../../headers/example_header.h"

    ?

    Also, is the syntax the same for this across every OS, or does it differ from XP to Ubuntu, for example?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by Programmer_P View Post
    What is the correct syntax in C++ to include a header in a child directory of a parent directory of the current directory?

    Is it this:

    #include "/../../example_header.h/headers"

    or is it this:

    #include "/../../headers/example_header.h"

    ?

    Also, is the syntax the same for this across every OS, or does it differ from XP to Ubuntu, for example?
    It does not differ from OS.

    Also, does a file have a folder, or does a folder have a file?

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, I tried using this syntax:

    #include "/.../../headers/example_header.h"

    like u implied is correct, by attempting to include a header file I know exists at that location, however, the compiler complained "No such file or directory".

    The source file I put the include line is called "CtextToSpeechDialog1.cpp" and it is located in a folder called "CtextToSpeechDialog1", which is in turn located in a folder called "module" which is in turn located in a folder called "TextToSpeechDialog1" which is in turn located in a directory called "modules". Now, the "modules" directory contains several other modules' directories, one of which is "TextToSpeechDialog2" (the next dialog module, which I'm trying to include in the first dialog's module "CtextToSpeechDialog1.h"). So, from the "modules" directory, I move "down" one directory into "TextToSpeechDialog2" which contains the header file which I then include, "TextToSpeechDialog2.h".

    Hence, the include line looks like this:

    #include "/../../../TextToSpeechDialog2/TextToSpeechDialog2.h"

    because I have to move "backwards" (or UP) 3 directories from the current directory into the "modules" folder which contains the "TextToSpeechDialog2" module folder. Now, tell me why it doesn't work...
    Last edited by Programmer_P; 04-30-2010 at 05:57 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have two contradictory bits in your post. First you state that the "module" directory is one directory up from where you are (since your .cpp file is in CtextToSpeechDialog1, which is a child of module), and then later you say that the module is three directories up from where you are. Both of these cannot be true at the same time.

    Also note the difference between .. (which makes sense) and ... (which doesn't).

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    You have two contradictory bits in your post. First you state that the "module" directory is one directory up from where you are (since your .cpp file is in CtextToSpeechDialog1, which is a child of module), and then later you say that the module is three directories up from where you are. Both of these cannot be true at the same time.

    Also note the difference between .. (which makes sense) and ... (which doesn't).
    That's because they're not. There are two different directories which have very similar names. One is named "module" and the other is named "modules". I explained it clearly in my last post, but you appear to have got it confused in your mind. "CtextToSpeechDialog1" (which contains the source "CtextToSpeechDialog1.cpp") IS located in a directory called "module". However, it in turn is located in a directory called "TextToSpeechDialog1". Now, one more directory up (i.e. "TextToSpeechDialog1"'s parent directory) is located the "modules" (plural, notice the "s" at the end) directory. Now, in this "modules" directory is located "TextToSpeechDialog2" which is where the header I'm including is located.

    Now tell me which part makes sense and which doesn't...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes you're right. Oops. Now for the real answer: This makes sense
    Code:
    "../../../more/stuff.h"
    This does not (it's legal, but does not make sense):
    Code:
    "/../../../more/stuff.h"
    / is the root directory and does not have any parents.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Thank you!
    That worked. Funny how much difference a single forwards slash at the beginning can make.
    And actually, the second one is not legal with my particular compiler, it seems.
    Last edited by Programmer_P; 04-30-2010 at 06:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  3. Header file include order
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2006, 03:22 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM