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.