Compiler Directives

Any line in source code with leading # is taken as a compiler directive. The initial # can be preceded or followed by whitespace (excluding new lines). The compiler directives are not case sensitive.

You can use conditional compilation to select particular sections of code to compile while excluding other sections. All compiler directives must be completed in the source file in which they begun.

Directives #DEFINE and #UNDEF

Use directive #DEFINE to define a conditional compiler constant (“flag”). You can use any identifier for a flag, with no limitations. No conflicts with program identifiers are possible because the flags have a separate name space. Only one flag can be set per directive.

For example:

#DEFINE extended_format

Use #UNDEF to undefine (“clear”) previously defined flag.

Directives #IFDEF, #IFNDEF, #ELSE and #ENDIF

Conditional compilation is carried out by the #IFDEF and #IFNDEF directives. #IFDEF tests whether a flag is currently defined, and #IFNDEF if the flag is not defined; i.e. whether a previous #DEFINE directive has been processed for that flag and is still in force.

Directives #IFDEF and #IFNDEF are terminated with the #ENDIF directive and can have an optional #ELSE clause:

#IFDEF flag THEN
  block of code
[ #ELSE
  alternate block of code ]
#ENDIF

First, #IFDEF checks if flag is defined by means of #DEFINE. If so, only block of code will be compiled. Otherwise, alternate block of code in #ELSE (if any) will be compiled. #ENDIF ends the conditional sequence. The result of the preceding scenario is that only one section of code (possibly empty) is passed on for further processing. The processed section can contain further conditional clauses, nested to any depth; each #IFDEF must be matched with a closing #ENDIF.

Here is an example:

' Uncomment the appropriate flag for your application:
'#DEFINE resolution10
'#DEFINE resolution12

#IFDEF resolution10 THEN
  // <code specific to 10-bit resolution>
  #ELSE
    #IFDEF resolution12 THEN
      // <code specific to 12-bit resolution>
    #ELSE
      // <default code>
    #ENDIF
#ENDIF

Unlike #IFDEF, #IFNDEF checks if flag is not defined by means of #DEFINE, thus producing the opposite results.

Include Directive #I

The #I parameter directive instructs mikroBasic PRO for ARM to include the named text file in the compilation. In effect, the file is inserted in the compiled text right after the #I filename directive. If filename does not specify a directory path, then, in addition to searching for the file in the same directory as the current unit, mikroBasic PRO for ARM will search for file in order specified by the search paths.

To specify a filename that includes a space, surround the file name with quotation marks: #I "My file".

There is one restriction to the use of include files: An include file can't be specified in the middle of a statement part. In fact, all statements between the begin and end of a statement part must exist in the same source file.

See also Predefined Project Level Defines.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code