Thread: Reason behind multiple levels of header files?

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    21

    Reason behind multiple levels of header files?

    I understand putting functions and such into separate files and then including that file. What I don't understand is the logic behind a file with just function declarations, and then a separate file with definitions. I understand this is a newbie question, but there's only one way to become a wizard...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    Quote Originally Posted by Leam
    What I don't understand is the logic behind a file with just function declarations, and then a separate file with definitions.
    Suppose you have a header file like this:
    Code:
    #ifndef LEAM_H_
    #define LEAM_H_
    
    void foo(void) {}
    
    #endif
    If you include the header file twice in say, your source file containing the main function, well, there's no problem: the header inclusion guards will work. But suppose you include the header file in the source file containing the main function, and also another source file. Then, you would end up with an error because foo is defined twice.

    Of course, you could say: why include the header in a different source file? Why not have a single source file, then you include everything else? Well, this is theoretically possible, but when you are developing non-trivial programs it can be hard to do since the work may be divided across many developers, who then have to be very careful to avoid name conflicts even for helper functions, whereas with multiple source files, each source file could have helper functions declared static, upon which different developers can use the same function name for different functions as long as they are in separate translation units. Furthermore, it tends to be easier when developers are able to work on separate translation units, though it is possible to combine them later (e.g., SQLite has the notion of the amalgamation, which is basically a huge source file built by a script from the disparate source files that the developers actually worked on). Also, for various reasons developers may wish to avoid distributing the implementation of functions, but rather just provide a header and then have the library in binary form.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    21
    Ah...while enlightenment is still far away, perhaps I begin to see. I am a solo newbie on a learner's path, most of my programs are less that two screens of code. Still, trying to learn the right way.

    So, if programmer X defined foo() in x_foo.h and programmer Y defined foo() in y_foo.h, the main written by programmer A would only define foo() if LEAM_H_ was not defined, and it would define foo from the (first?) included file.

    I need to study some more.

    Addendum: I need more coffee as well. Still trying to get this. If the header for foo() was defined in x_foo.h, what would make the compiler call x_foo.c where the function was provided?
    Last edited by Leam; 01-24-2016 at 09:31 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    Quote Originally Posted by Leam
    If the header for foo() was defined in x_foo.h, what would make the compiler call x_foo.c where the function was provided?
    Remember that besides compiling individual source files, you link to build the final program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    21
    So, here's where I'm playing. The main function is in run_ship.c that calls lib/ship.c.
    https://github.com/LeamHall/ale_lcth.../run_ship.c#L2

    Which includes lib/ship.h which defines a multiplier. Should ship.h have the function declarations while the definitions are in ship.c? Or am I further afield than suspected?

    https://github.com/LeamHall/ale_lcth.../lib/ship.c#L5

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    21
    Quote Originally Posted by laserlight View Post
    Remember that besides compiling individual source files, you link to build the final program.
    Ah, maybe the small size of my tasks hides the problem the include process is trying to solve?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    To avoid problems, it is better to include header files only, and then ensure that whatever is in the header files will not cause errors should the headers be included in different source files, or if they are included in various possible orderings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 08-29-2011, 01:35 PM
  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, 10:15 PM

Tags for this Thread