2

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.

4

5 回答 5

10

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.

于 2012-12-19T20:53:07.633 回答
6

You usually define (not only declare) inlined functions in headers.

And you declare non-inlined functions (e.g. functions whose body is large enough), and define them (i.e. implement them) in one particular compilation unit.

Then the linker resolves the appropriate function names (usually mangled names) at link-time. And you don't want to have multiply defined functions.

Having a function definition provided only by one compilation unit makes the total build time a bit faster.

With link-time optimizations (e.g. the -flto option to g++ both during compilation and during linking) things become more complicated.

Notice that huge software (some executables are nearly one gigabyte of binary, and simply linking them takes several minutes) bring constraints that a lone programmer don't even imagine. Just try to compile a large free software (Libreoffice, Firefox, Qt5, ...) from its source code to guess the issues.

BTW, you could in principle put all the code of some program in a single source file, but for valid and obvious reasons people don't do that.

于 2012-12-19T20:53:33.987 回答
4

Putting function definitions into the header causes lengthy compile times when using any non-trivial system. It may work for small projects to make everything inline but it certainly does not work for bigger systems (not to mention large systems with a couple of hundred million lines of code).

于 2012-12-19T20:55:12.820 回答
1

The function declarations inside a header file provide symbol references that can be used to link code together. When you compile individual source files, each one gets generated into object code.

If you want to use one source's functions in another source file, you need some way to know where that code is and how to call it. The compiler is able to use the function declarations for just this reason. It knows how to call it and what it returns, but it doesn't yet know where the source for the function is.

Once all the sources are compiled into object code, the linker then assembles all the object files into an executable (or library), and these symbol references are resolved.

于 2012-12-19T20:56:31.050 回答
0

If you have all the code in a single, non-shared file it really doesn't matter where it is - in header or in source file.

There are 2 main reasons for splitting the code to headers and source files. They can be summarized like this:

  • Technical. If you have several source files interacting with each other, you need to include the headers. If you define everything in the header, you'll have multiple definitions of you code included - and that's a compilation error.

  • Design. Header files define the interface of your software which can be distributed to client software without exposure of the internal implementation.

于 2012-12-19T20:59:21.577 回答