Like Tree1Likes
  • 1 Post By tabstop

Where Should I Put Header Calls When Using Multiple Files?

This is a discussion on Where Should I Put Header Calls When Using Multiple Files? within the C Programming forums, part of the General Programming Boards category; Suppose I am working on a program with 3 files: Main.c (main file which runs the program) Functions.c (collection of ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    33

    Where Should I Put Header Calls When Using Multiple Files?

    Suppose I am working on a program with 3 files:

    1. Main.c (main file which runs the program)
    2. Functions.c (collection of functions used by main.c)
    3. MyHeader.h (list of function prototypes, global variables, typedefs and what not)


    Suppose also that I need to use both the stdio.h and stdlib.h libraries in this program.

    I am used to put only a #include "MyHeader.h" on both main.c and functions.c, and then put #include <stdio.h> and #include <stdlib.h> on MyHeader.h (this seems to me the most logic and not-redundant solution).

    But, I have seen some very experienced programmers put no #includes at all on MyHeader.h, and then put them as needed on main.c and functions.c, so in this example MyHeader.h would have no #includes, main.c would have:

    #include <stdio.h>
    #include "MyHeader.h"

    and functions.c would have:

    #include <stdio.h>
    #include <stdlib.h>
    #include "MyHeader.h"

    As you can see with this second method stdio.h is included twice, while with my method only once.

    So which is the best/recommended way to call headers (if there is one)?
    Last edited by envec83; 08-27-2011 at 06:10 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You put them in the files where you need the information that's in them.
    You don't put them in the files where you don't.

    It all depends what you are doing.

    As for the called twice problem, you use include guards...
    Code:
    #ifndef MYHEADER_H
    #define MYHEADER_H
    
    // put your stuff here
    
    #end if // MYHEADER_H
    The first time through the HYHEADER_H is not defined, the #define creates it and the contents of the header are included. The second time through the MYHEADER_H is defined and the rest is skipped.

    As a general rule, I try to make the #include list at the top of a page as explicit as possible. That is, I don't include anything I don't absolutely need. I also don't put long lists of #includes in headers if I can get away from it... again to keep everything as explicit as possible.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Suppose also that I need to use both stdio.h and stdlib.h in this program.

    I am used to put only a call to MyHeader.h on both main.c and functions.c, and then put calls for stdio.h and stdlib.h on MyHeader.h (this seems to me the most logic and not-redundant solution).

    But, I have seen some very experienced programmers put no header calls at all on MyHeader.h, and then put them as needed on main.c and functions.c, so in this example MyHeader.h would have no calls, main.c would call stdio.h and MyHeader.h, and functions.c would call stdio.h, stdlib.h and MyHeader.h (making stdio.h called twice).

    So which is the best/recommended way to call headers (if there is one)?
    First of all.. What do you mean by "call headers" ?
    If you mean putting code that has function calls(..or any other sort) , in the header; you've got it wrong.

    Consider this example:
    X.h
    Code:
    //Guard if needed
    void foo(void);
    X.c
    Code:
    #include "X.h"
    void foo(void)
    {
       //Do whatever you like 
    }
    main.c
    Code:
    #include "X.h"
    int main(void)
    {
       foo();
       return 0;
    }
    Now try to explain your question w.r.t that..

    [Edit: Sorry..just saw the last post...
    @envec83: If you asked about the guards, look at Tater's explanation]
    Last edited by manasij7479; 08-27-2011 at 05:33 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    But, I have seen some very experienced programmers put no header calls at all on MyHeader.h, and then put them as needed on main.c and functions.c,
    The reason you see them do it that way is because myheader.h is not using any types, from <stdio.h> or <stdlib.h>, as arguments to functions in function prototypes.

    So which is the best/recommended way to call headers (if there is one)?
    It really depends on what the header is. For example, if any of the prototypes in myheader.h use FILE * arguments, or if a structure defined in myheader.h has a FILE * member, then writing #include <stdio.h> would be appropriate. You should include headers in any files that use functions or types from those headers.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Edit: Sorry..just saw the last post]
    Which does not negate what you were saying...

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    Quote Originally Posted by manasij7479 View Post
    First of all.. What do you mean by "call headers" ?
    If you mean putting code that has function calls(..or any other sort) , in the header; you've got it wrong.
    I don't mean that, and yeah I know that you shouldn't put function calls or definitions inside a header file. If you read my first post you'll notice that I said functions go inside the functions.c file.

    What I am asking is where I put the #includes.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by envec83 View Post
    What I am asking is where I put the #includes.
    At the top.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If it's just you, I suppose no one really cares where your includes are.

    But the guidelines for #include is supposed to make your code play well with others (and in this case, others can be "you, a year from now") -- don't include things you don't need, because everything you put in your .h file gets pasted into the .c files (that's what #include does), and it's a bit rude to dump <stdlib.h> on someone when they don't actually use anything in that header.
    stahta01 likes this.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    Quote Originally Posted by tabstop View Post
    If it's just you, I suppose no one really cares where your includes are.

    But the guidelines for #include is supposed to make your code play well with others (and in this case, others can be "you, a year from now") -- don't include things you don't need, because everything you put in your .h file gets pasted into the .c files (that's what #include does), and it's a bit rude to dump <stdlib.h> on someone when they don't actually use anything in that header.
    The fact that you should not include libraries you don't need is pretty obvious to me.

    What I am asking is where to place the ones you do need, because there are several ways to do it (I mentioned two). And yep I am wondering this because my code might need to be used by other people, or by myself one year from now as you correctly pointed out.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by envec83
    What I am asking is where to place the ones you do need, because there are several ways to do it (I mentioned two). And yep I am wondering this because my code might need to be used by other people, or by myself one year from now as you correctly pointed out.
    No one is going to be bothered or confused by an extra #include in a .c(or .cpp or .h ..or whatever) file especially if you need it and the headers are guarded well.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Quote Originally Posted by envec83
    The fact that you should not include libraries you don't need is pretty obvious to me.

    What I am asking is where to place the ones you do need, because there are several ways to do it (I mentioned two). And yep I am wondering this because my code might need to be used by other people, or by myself one year from now as you correctly pointed out.
    The two ways that you mentioned are basically:
    1. Place them all in a single header which is then included in the source files.
    2. Place them in the header if the header needs them, otherwise place them in the source files that need them.

    When your header file sees more use, #1 may result in including header files that whoever included your header file does not need. Sure, they need to be included in your source files, but not in those other source files. However, because at the moment you are only thinking small, i.e., all the source files that you have in mind really do need those includes, you thus think that what tabstop mentioned is not relevant to your question, when actually it answers your question.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    Quote Originally Posted by laserlight View Post
    The two ways that you mentioned are basically:
    1. Place them all in a single header which is then included in the source files.
    2. Place them in the header if the header needs them, otherwise place them in the source files that need them.

    When your header file sees more use, #1 may result in including header files that whoever included your header file does not need. Sure, they need to be included in your source files, but not in those other source files. However, because at the moment you are only thinking small, i.e., all the source files that you have in mind really do need those includes, you thus think that what tabstop mentioned is not relevant to your question, when actually it answers your question.
    Gotcha now.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. for loop that calls multiple functions
    By elsparko in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2009, 06:10 AM
  2. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 05:59 PM
  3. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM
  4. Multiple Classes And Header Files error does not name a type
    By rainmanddw in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2006, 11:17 AM
  5. Multiple header files , cout undelcared probem
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2005, 09:15 PM

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