Blackdragon1400
Blackdragon1400

Reputation: 423

#include and what actually compiles

This is just a general compiler question, directed at C based languages.

If I have some code that looks like this:

#include "header1.h"
#include "header2.h"
#include "header3.h"
#include "header4.h"  //Header where #define BUILD_MODULE is located

#ifdef BUILD_MODULE

//module code to build

#endif //BUILD_MODULE

Will all of the code associated with those headers get built even if BUILD_MODULE is not defined? The compiler just "pastes" the contents of headers correct? So this would essentially build a useless bunch or header code that just takes up space?

Upvotes: 0

Views: 92

Answers (3)

Eric Postpischil
Eric Postpischil

Reputation: 224082

All of the text of the headers will be included in the compilation, but they will generally have little or no effect, as explained below.

C does not have any concept of “header code”. A compilation of the file in the question would be treated the same as if the contents of all the included files appeared in a single file. Then what matters is whether the contents define any objects or functions.

Most declarations in header files are (as header files are commonly used) just declarations, not definitions. They just tell the compiler about things; they do not actually cause objects or code to be created. For the most part, a compiler will not generate any data or code from declarations that are not definitions.

If the headers define external objects or functions, the compiler must generate data (or space) or code for them, because these objects or functions could be referred to from other source files to be compiled later and then linked with the object produced from the current compilation. (Some linkers can determine that external objects or functions are not used and discard them.)

If the headers define static objects or functions (to be precise, objects with internal or no linkage), then a compiler may generate data or code for these. However, the optimizer should see that these objects and functions are not referenced, and therefore generation may be suppressed. This is a simple optimization, because it does not require any complicated code or data analysis, simply an observation that nothing depends on the objects or functions.

So, the C standard does not guarantee that no data or code is generated for static objects or functions, but even moderate quality C implementations should avoid it, unless optimization is disabled.

Upvotes: 4

Hot Licks
Hot Licks

Reputation: 47749

Conceptually, at least, include/macro processing is a separate step from compilation. The main source file is read and a new temporary file is constructed containing all the included code. If anything is "#ifdefed out" then that code is not included in the temporary file. At the same time, the occurrences of macro names are replaced with the text they "expand" into. It is that resulting file, with all the includes included, etc, that is fed into the actual compiler.

Some compilers do this literally (and you can even "capture" the intermediate file) while others sort of simulate it (and actually require an entire separate step if you request that the intermediate file be produced). But most compilers have one means or another of producing the file for your examination.

The C/C++ standards lay out some rather arcane rules that must be followed to assure that any "simulated" implementation doesn't somehow change the behavior of the resulting code, vs the "literal" approach.

Upvotes: 1

drz
drz

Reputation: 992

Depends on the actual compiler. Optimizing compilers will not generate the output for unrequired code, whereas dumber compilers will.

gcc (a very common c compiler for open-source platforms) will optimize your code with the -O option, which will not generate unneeded expressions.

Code in #ifdef statements where the target is not defined will never generate output, as this would violate the language specifications.

Upvotes: 1

Related Questions