Include file in a different location

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vjsarthy
    New Member
    • Feb 2008
    • 5

    Include file in a different location

    How do I include a include file which is in a different folder and the .c file where I am calling this file is in a different folder?

    Lets say I want to include sample.h file which is in a different folder.

    is it like #include "c:\difffolder\ sample.h" ?

    Is it same syntax for both windows and unix based compiler?

    Please advise.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Most (if not all) compiler preprocessors implement the -I<dirname> command
    line flag. <dirname> is a directory where your include files are stored.

    kind regards,

    Jos

    Comment

    • fual
      New Member
      • Feb 2008
      • 28

      #3
      If you want to do this in a portable way you need to use relative locations:
      Code:
      #include "../dir_name/file.h"
      will take you down a level for example.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by fual
        #include "../dir_name/file.h"
        What's better is to use #include <file.h>.

        All that's required is that you pre-define the path to the header.

        The #include "file.h" looks in the PWD (present workign directory) and then looks doen the pre-defined paths.

        I would always avoid a relative path unless I were absolutely guaranteed there would be no future situation that coudl occur that would force me to go into dozens, maybe hundreds, maybe thoussand of source files and change relative paths.

        Comment

        • fual
          New Member
          • Feb 2008
          • 28

          #5
          You make a very good point.

          \me quickly scurries off to modify some code

          Comment

          Working...