Dunfield Development Services Inc.
APPLICATION NOTE
Home
Products
Ordering & Status
Free Download Files
Links
Notices
Contact Us

 

Dunfield Development Services Inc.
Customer Support -- Application Note #0009

MICRO-C
Using conditionals within MCP macros

Applies to: [Micro-C Compiler ]
Last updated: Sunday May 04, 2003 .
Application Note Index

Back ] Next ]

PROCEDURE

The MCP preprocessor provided with MICRO-C is a fairly complete C pre-processor, with enough capability to satisfy most programmers requirements. It is however a simplified version of the standard C preprocessor, and one capability that is not supported (and occasionally requested) is the ability to embed conditional statements within multi-line preprocessor macros, usually for the purpose of providing debugging or compilation specific versions of those macros.

MCP does not process conditional statements within macro definitions. Here are three possible work-arounds. The first two work by moving the conditional outside of the macro, the third works by relying on the limited internal preprocessor built into the compiler to process the conditional statements
which are recorded in the macro definition.

------------------------------------------------------------------------

// Method #1 - Use different header files
// - Most obvious and simplest method to handle different macros
// - Conditional is evaluated only one for ALL macros
// - Uses normal MCP defines and constants
// - Can use #if/#ifdef/#ifndef (supported by MCP preprocessor)
#define DEBUG

#ifdef DEBUG
#include <debug.h> // Get debugging macros
#else
#include <standard.h> // Get production macros
#endif

------------------------------------------------------------------------

// Method #2 - Place entire macro definitions within a conditional block
// - Conditional is evaluated only once when the macro is defined
// - Uses normal MCP defines and conditionals
// - Can use #if/#ifdef/#ifndef (supported by MCP preprocessor)
#define DEBUG

#ifdef DEBUG // Define macro version for DEBUG compilation
#define macro(parm) {\
debug code\
macro code\
}
#else // Define macro version for NORMAL compilation
#define macro(parm) {\
macro code\
}
#endif

------------------------------------------------------------------------

// Method #3 - Pass macro conditional on to internal preprocessor in MCC
// - Conditional is evaluated AFTER macro is expanded
// - Must use a "special" deferred #define for the control,
// not processed by MCP like "normal" macros and defines.
// - Can only use #ifdef/#ifndef (supported by MCC compiler)

// Leading comment forces MCP to miss (defer to MCC) the #define
/**/#define DEBUG

// Conditionals in MACROS are not processed by MCP, and will therefore
// be passed on to the internal preprocessor after macro expansion */
#define macro(param) {\
#ifdef DEBUG\
debug code\
#endif\
macro code\
}

 

 

Back ] Next ]

© 2008 Dunfield Development Services Inc.     View our Privacy Statement
Refunds given at the discretion of management.
 Last Updated:  Tuesday, March 18, 2008