Using MACRO Definitions

By using MACRO definitions, you can easily write lines of code that are only included in Debug versions and not in Release versions. For instance, if you create a function ‘MyLog(x,y)’ that writes debugging information to a file you can define a macro such as:

 #ifdef DEBUG#define LOG(a,b)   MyLog(a,b);#else#define LOG(a,b)#endif

Using conditional compilation, you can remove the ‘MyLog’ code from your Release version. However, in case of logging, you want to pass a variable amount of parameters most of the time. In this case the macro definition mentioned above doesn’t work. Nevertheless, there is a nice workaround. The solution is to redefine the macro as:

 #ifdef DEBUG#define LOG(a)   MyLog a;#else#define LOG(a)#endif

In your code you should use it as:

 …LOG ((a,b,c))…LOG ((a,b,c,d,e))…

By putting the parameters between double brackets, you can pass a variable amount of parameters to your macro.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: