Reputation: 15284
Doea GCC compile all functions including the unused ones?
When we include a header or library, does the compiled excutable include all functions from the library?
Upvotes: 0
Views: 933
Reputation: 70126
(1) There are functions that you declare. These are the functions that the compiler knows about, but it does not necessarily know (or have to know) what they look like on the inside (it does know what parameters they expect and what they return).
When you include a header, the header usually contains mostly declarations, and sometimes a few inline functions. That means that for the most part, the compiler knows about but does not need to compile the functions at that time (with the exception of inline functions, since an implementation is provided for these). It will pretend that the functions exist, since you "promised" that they do.
There are functions that you declare and implement. Usually, but not necessary, these functions are implemented in the source file that includes the header declaring them. The compiler must compile all functions that are implemented. Further, some of them may be inlined or instantiated in several places or with different parameters, possibly generating slightly or entirely different executable code in the end.
On the other hand, there are functions that you declare but do not implement. The compiler does not care, it can just pretend that they are present, even when they are not (as long as they are declared).
However, if they are used the binary code for these must come "from somewhere" at link time, most commonly a static or dynamic library. If the linker cannot find the implementation anywhere, it will fail to build the executable and generate a link error.
(2) What is compiled and what is finally placed in the executable are vastly different things. Building an executable is a many-step process.
First, every compilation unit (read as: source file) is run through the preprocessor. The preprocessor is a pure text replacement automaton which has no knowledge of the actual programming language. A preprocessor directive such as #include
causes the text from another file being inserted at that position, without any understanding of what the meaning of this text is.
Next, the compiler checks the syntax, and builds a data structure that represents the meaning of your program. This naturally presumes compiling every bit of code that is handed to it. However, not all functions must necessarily be implemented. Optionally, the compiler performs a number of optimizations and then generates some form of executable code (pseudo-code, assembler source, whatever). This may include calls to functions that the compiler only formally knows about because it has seen their declaration.
Finally, the linker takes all object files and optionally some libraries and puts together an executable using all functions that are used. It may optionally perform another optimization pass.
Usually, only functions that are actually used are put into a binary (although there is no strict guarantee for that). The functions are either copied from the object files that the compiler generated while compiling your source code, or from a static library.
For functions that come from a dynamic library, only the necessary information where the function is found as well as some small glue code (usually a jump instruction followed by a return, this is used so the loader can easily relocate) is added to the executable, the actual function is loaded from the dynamic library when the program runs.
Note that a function may be inlined so it factually appears several times (identically or slightly different) and a function may be both inlined and linked as normal function. Or, it may be placed in the executable even though you never call it (for example because you take its address). Or, at the other extreme, a function may be completely optimized out, so it does not appear in the executable at all even though it is used.
Upvotes: 4
Reputation: 4847
No, in general the executable only contains the functions that are needed for execution. However, when in doubt the compiler errs on the safe side, this means there is a good chance your executable carries a lot of unnecessary code, this is mostly when either the compiler/linker isn't sure if some code is needed, or when it's unable to split off the unneeded code, like when it's in the same object file as some required function.
Upvotes: 0
Reputation: 4243
Yes, all functions are compiled. But:
—they may be stripped before being linked (if you've turned that on as a compile setting) or
—not linked into an executable at all (if your build settings don't include such a command in a linker phase.
Upvotes: 3