using relative file paths..?

This is a discussion on using relative file paths..? within the C++ Programming forums, part of the General Programming Boards category; Hi I've got an include statement such as: Code: #include "c:\object.h" My question is that can I use relative file ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    17

    using relative file paths..?

    Hi

    I've got an include statement such as:

    Code:
    #include "c:\object.h"
    My question is that can I use relative file paths, so that if my source code is in another folder it can find the header by using a relative path such as: ../../object.ch?

    I have tried this, but it doesn't seem to work. Is there a particular way to do this?

    Thank you.

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Probably a bad idea...

    Well, it's a backslash ..\ , not a forward-slash ../ to go up one level. A backslash without the .. should take you down from the curent path. But, since this does not change the default directory, the .h file won't find the associated .cpp or .lib file.

    Also, using relative directories (or any specific directory) will make your code less portable.

    You can add a directory to the default path, using the DOS path command. The system will look in all those places for the file. Type path at the command prompt to see the current paths. I dunno where the path is set-up in WinXP. In Win98 it's in the autoexec.bat file. And, you would edit the autoexec.bat file, adding your path to the statement. Don't just type PATH C:\MyPath, or you will wipe-out the current paths
    Last edited by DougDbug; 11-25-2003 at 03:29 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Should work, providing you get the / or \ right (as per your OS)

    Also, you need to be sure of what your compiler thinks is the current directory.

    The usual way is to
    Code:
    #include "other.h"
    And compile with
    gcc -I../h prog.c

    That is, you tell the compiler on the command line where all the relative directories to your header files are.

    Rather than
    Code:
    #include "../h/other.h"
    And compile with
    gcc prog.c

    In say vc++, you specify additional include directories in the project's compiler settings.


    Relative paths in #include's can be problematic when porting code from one system to another, and they reduce your ability to re-arrange your project as the code base grows (imagine editing a load of files to change some paths).


    > You can add a directory to the default path, using the DOS path command
    No - that is the program search path, not the compiler include path.

    If it exists at all, it is usually called INCLUDE (or perhaps INC)

    But since it (as an environment variable) affects everything you compile, it's usually reserved for all the system include directories.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  4. Newbie Question: File Input and Relative Paths
    By Ashes999 in forum C++ Programming
    Replies: 11
    Last Post: 05-23-2003, 05:21 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21