Question about including header files

Discussion in 'C Programming' started by KIRAN, Oct 10, 2008.

  1. KIRAN

    KIRAN Guest

    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
     
    KIRAN, Oct 10, 2008
    #1
    1. Advertising

  2. KIRAN

    Ian Collins Guest

    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
     
    Ian Collins, Oct 10, 2008
    #2
    1. Advertising

  3. 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.
     
    Antoninus Twink, Oct 10, 2008
    #3
  4. KIRAN

    James Kuyper Guest

    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.
     
    James Kuyper, Oct 10, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eternally
    Replies:
    7
    Views:
    425
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    639
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,361
    Walter Roberson
    May 1, 2006
  4. shruti

    problem including perl header files

    shruti, May 15, 2006, in forum: C Programming
    Replies:
    2
    Views:
    484
    Keith Thompson
    May 15, 2006
  5. mlt
    Replies:
    2
    Views:
    917
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page