This question is from an idealistic standpoint. // Forward declaration
I've been learning the basics of makefiles and I found myself wondering the same thing that was asked here about header dependencies. From a practical standpoint, I'm glad that there are solutions which solve the problem of header dependencies. However, thinking on the question a bit more, I find myself asking why a header dependency would ever exclusively affect a compilation unit. Under the assumption (see forward declaration) that definitions are contained in headers and implementation is defined in source units, is there any reason that modification to a header file is merited without modification to the respective source file? If not, why does it even matter to recompile the dependent source unit(s) since a definition modification alone will have no affect on program execution? Is it just a question of identifying errors in changes to interface definitions sooner rather than later?
1 Answer 1
(To be precise, declarations are contained in header files, and definitions (i.e. implementations) are in source files, but that's just terminology.)
"Is there any reason that modification to a header file is merited without modification to the respective source file?"
Yes. Changing a member from public to private, for example. Also, more than one source file can #include a given header file, so a change to the header might require a change to one of them but not another. For example, adding/removing/renaming a data member in a class might not require any change in the the classes implementation, but still require a change in other source code that uses the class.
Even when a source file hasn't changed at all, it can still be logically necessary to recompile it if a header file has changed. For instance, adding/removing a data member changes the size of a class, which means that any code that uses instances of that class must adjust the amount of memory to allocate for them.