Thread: Header Files / Including

  1. #1
    Registered User FlamingAhura's Avatar
    Join Date
    May 2010
    Posts
    5

    Header Files / Including

    Hey everyone, I'm hoping you can help me with a simple question I've never been able to find a clear answer for.

    Basically, let's just assume I'm making a program and have the following files:
    Code:
    a.h - defines/prototypes/typedefs for code in a.c
    a.c - code
    
    b.h - defines/prototypes/typedefs for b.c
    b.c - code
    
    common.h - defines and typedefs used in all code files
    
    main.c - main() / other code
    1. Does this make sense?
    2. Assuming I have include guards and am compiling this into a single executable, where should my includes be? For example, I feel that I need to "#include a.h" in a.c, but what about "#include a.h" in main.c if I'm calling the functions there? And where should I include common.h?
    Last edited by FlamingAhura; 11-03-2010 at 08:46 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You only need to include header files in files which need a definition from the header file. So if main.c is calling functions in a.c, and the prototypes for those functions are in a.h, then you should include a.h in main.c.

    You should include common.h in every file that depends on something defined within it. You can use a trick if you want to avoid having things defined multiple times by the preprocessor. It would be something like this:
    Code:
    /* Contents of common.h */
    #ifndef COMMON_H_
    #define COMMON_H_
    
    /* put a bunch of definitions here...*/
    
    #endif
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User FlamingAhura's Avatar
    Join Date
    May 2010
    Posts
    5
    Thanks a lot, I'll be sure to do it like that. Hopefully I can get such basic organization down so I can finally focus on the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  3. Including header files
    By Emeighty in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2008, 03:02 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM

Tags for this Thread