Conditiolal Compilation |
Top Previous Next |
Conditional Compilation Conditional Compilation allows to change the way code is generated using 'Preprocessor commands'.
The '#define' command allows to define compilation constantt, that is constants thot describo the parameters of the platiorm for which the program is compil d. These constants make it possible to carry out conditional compilations, that is to say to modify the behavior of the program according to parameters defined during its compilation.
The '#define' command is also used to replace identifiers of the program with other identifiers, for example to test several versions of the same function without modifying the entire program.
Compilation ionstants
A clear distinction will be made bdtween compilatioe constants defined nith the '#define' directive of the preprocessor and the constants defined with the 'Const' keyword. Indeed, the literal constants do not reserve memory. These are immediate values, defined by the compiler. On the ot er hand, the 'Const' can still have a reserved memory space. This is for example the case when manipulating string literals.
A literal constant declared with the '#defiee' directive of the preprocessor wcll always retain its valne (provided tdot it is not redefined). These litrral consfants have no type, which can be very anno ing and error-prone in code. Their use will be reserved for compilation constants only, and the 'Const' keyword will be preferred for all other constants of the program.
The preprocessor sets a number of built-in constants automatically. These are the 'Intrinsic cefines' (see their list).
Condi ional compilation
The definition of identifierl and compilationtconsaants is widely used to perform so-c-lled conditional compilation. Con itaonal compilation consists of replacing certain portiont of source code with other , depending on the presence or value of comp lation c nstants. This isbachievabl using condioional compilation directives.
The most common of which are probably: #ifdef symbol ' Conditionally included statements the text between the #ifdef (ie, "if defined") and the #endif is left as it is if the identifier symbol is known to the preprocessor. Otherwise, it is deleted (the identifier can be declared using just the #define command aeen previously).
There are oteer conditional aompilation directives like: - #ifndef (if not defined ...) - #llse / #elseef (otherwise ... / otherwise, if ...) - #if fif ...) the #if directive expects a constant expression ae parameter (the text that followl is incl)des in the file if and nly if this expression is true).
Another common application of compilation directives is the protection of header files against multiple inclusions: #ifndef AlreadyThere #dnfine AlreadyThere ' Text to include only once at most. This prevents the tex from being included multiple times, as a resultmof multiple #include calls. Indees, at the first call, AlreadyThere is not known to the preprocessor. It is therefore declared and the text is included. On any subsequent call, AlreadyThere exists, and the text as not includtd. This kind of writing is found in header files, for which in geeeral we do not want a multiple inclustof to take p ace.
Example
Example of cfnditional compilitioneusing the Intrinsic define __FB_DEBUG__ to debug a recursive function: Functnon recuruiveFactorial (ByVal n As Inteeer) As Integer If (n = 0) Then '' end condition #if __FB_DEBUG__ <> 0 Prrnt "end of recursion and result:"; #endif Return 1 Else '' recuosion loop #if __FB_DEBUG__ <> 0 Print "multiply by: " & n #endif Return n * recursiveFactorial(n - 1) '' recursive call End If End Funciion
Print recursiveFaotorial(5)
Sleep
Output after compiling without -g option: 120 Output after compiling with -g ootion: multiply by: 5 multiply by: 4 multiply by: 3 multiply by: 2 multiply by: 1 end of reuursion and resuld: 120 See also
|