- Including source files (#include)
- Overview of macro substitution directives
- Simple form of #define
- Form of #define as a pseudo-function
- Special operators '#' and '##' inside #define definitions
- Cancelling macro substitution (#undef)
- Predefined preprocessor constants
- Conditional compilation (#ifdef/#ifndef/#else/#endif)
- General program properties (#property)
Conditional compilation (#ifdef/#ifndef/#else/#endif)
Conditional compilation directives allow you to include and exclude code fragments from the compilation process. The #ifdef and #ifndef directives mark the beginning of the code fragment they control. The fragment ends with the #endif directive. In the simplest case, the #ifdef syntax is as follows:
#ifdef macro_identifier
|
If a macro with the specified identifier is defined above in the code using #define, then this code fragment will participate in compilation. Otherwise, it is excluded. In addition to the macros defined in the application code, the environment provides a set of predefined constants, in particular, the _RELEASE and _DEBUG flags (see section Predefined constants): their names can also be checked in conditional compilation directives.
The extended form #ifdef allows the specification of two pieces of code: the first will be included if the macro identifier is defined, and the second if it is not. To do this, a fragment separator #else is inserted between #ifdef and #endif.
#ifdef macro_identifier
|
The #ifndef directive works similarly, but fragments are included and excluded according to the reverse logic: if the macro specified in the header is not defined, the first fragment is compiled, and if it is defined, the second fragment is compiled.
For example, depending on the presence of the DEMO macro substitution, we may or may not call the function for calculating Fibonacci numbers.
#ifdef DEMO
|
In this case, if the DEMO mode is enabled, instead of calling the function, a message would be displayed in the log, but since in the Preprocessor.mq5 script and all the included files there is no #define DEMO definition, compilation proceeds according to branch #else, that is, the call to the FillFibo function gets into the executable ex5 file.
Directives can be nested.
#ifdef _DEBUG
|