Thread: Many Header Files

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    Many Header Files

    I am developing an application using MSVC++ that is getting somewhat large. I make a new source and header file for each task I need to carry out. I don't know where to include all the header files. Currently, I put a header files in each source file that will call a function from it. It seems I may be repeating where and how often the header file should be included. Is there a rule / guideline? Can I have ONE header file which includes all the other header files I wrote. And that ONE header file will be included in the rest of the source files??? THANKS

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by matth
    It seems I may be repeating where and how often the header file should be included.
    Typically this is dealt with through the use of inclusion guards:

    file1.h
    Code:
    #ifndef FILE1_H
    #define FILE1_H
    
    // Put your prototypes, etc. here
    
    #endif
    file2.h
    Code:
    #ifndef FILE2_H
    #define FILE2_H
    
    // Put your prototypes, etc. here
    
    #endif
    etc... for each header file.

    This helps break the recursion that can sometimes develop if header file 1 includes header file 2 which includes header file 1 which includes header file 2 which includes header file 1, etc.........
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Quote Originally Posted by matth
    Can I have ONE header file which includes all the other header files I wrote. And that ONE header file will be included in the rest of the source files???
    Probably, look at windows.h as an example of a single file that includes multiple files that in turn include multiple files, etc. It's debateable in my mind whether the inclusion of files behind the scenes is better or worse than a long list of inclues in a given project, but if you are including the same list of headers in project after project after project, then I believe the technique you describe is legal.
    You're only born perfect.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by matth
    Can I have ONE header file which includes all the other header files I wrote. And that ONE header file will be included in the rest of the source files???
    Yes. Download the SMAUG codebase and you'll see that they do the same thing. As a matter of style, I don't like it at all. It makes it tough to see what functions are in what object files. I prefer one header per each source file.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

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. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM