Question about including header files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KIRAN

    Question about including header files

    Hello all,
    My question is about the way of including header files(*.h) in source
    files (*.c)
    I have three folders,
    -build ( for project makefiles)
    -include ( for *.h files)
    -src (for *.c files).

    I know that there are two ways of specifying include path of header
    files
    1. using -I flag at the compilation ( for gcc)
    2. including *.h files in source file with complete relative path
    (Ex. to include test.h in test.c I would write #include "../include/
    test.h" in test.c)

    Which is the most preferred method of the above two?
    Suggestions/help is apprecited. Thanks in advance.
    Regards,
    Kiran
  • Ian Collins

    #2
    Re: Question about including header files

    KIRAN wrote:
    Hello all,
    My question is about the way of including header files(*.h) in source
    files (*.c)
    I have three folders,
    -build ( for project makefiles)
    -include ( for *.h files)
    -src (for *.c files).
    >
    I know that there are two ways of specifying include path of header
    files
    1. using -I flag at the compilation ( for gcc)
    2. including *.h files in source file with complete relative path
    (Ex. to include test.h in test.c I would write #include "../include/
    test.h" in test.c)
    >
    Which is the most preferred method of the above two?
    Which ever requires the lest work to setup and maintain.

    I'd use compile options, then you only have to change the makefile(s) if
    your layout changes.

    --
    Ian Collins

    Comment

    • Antoninus Twink

      #3
      Re: Question about including header files

      On 10 Oct 2008 at 2:55, KIRAN wrote:
      I know that there are two ways of specifying include path of header
      files
      1. using -I flag at the compilation ( for gcc)
      2. including *.h files in source file with complete relative path
      (Ex. to include test.h in test.c I would write #include "../include/
      test.h" in test.c)
      >
      Which is the most preferred method of the above two?
      Usually the first is simpler, and makes life easier if you ever decide
      to turn bits of the program into a library.

      Just put -I../include in your CPPFLAGS in your Makefile - do it once and
      you'll never have to think about it again.

      Comment

      • James Kuyper

        #4
        Re: Question about including header files

        KIRAN wrote:
        Hello all,
        My question is about the way of including header files(*.h) in source
        files (*.c)
        I have three folders,
        -build ( for project makefiles)
        -include ( for *.h files)
        -src (for *.c files).
        >
        I know that there are two ways of specifying include path of header
        files
        1. using -I flag at the compilation ( for gcc)
        This also works for most of the other C compilers I've used.
        2. including *.h files in source file with complete relative path
        (Ex. to include test.h in test.c I would write #include "../include/
        test.h" in test.c)
        >
        Which is the most preferred method of the above two?
        I definitely prefer the -I option. It gives you more freedom to
        rearrange the structure of your project without having to rewrite the
        source code. Moving the header file directory requires only the change
        of a single line in my make file, rather than a change to every source
        code file that #includes any file in that directory.

        Comment

        Working...