i know what is a header file,but,i still don’t understand why a lot of programmers make a header file,and a source file with the same name,and only prototype functions in the header file,while tell what the function does in the source file.
I never make functions and their prototypes in separate files,just stuff it all into the header file.
The question is,why make a source file for headers?Does it give any advantages?Is it just to make the code look cleaner?I don’t understand.
If you implement a function in a header, and then include the header into two or more different source files, you’ll have multiple definitions of the same function. This violates the one definition rule.
It’s possible to get around that by declaring the functions inline, but (unless the linker knows how to merge the multiple definitions) that can lead to code bloat.