一般在调试过程中,需要调试开关来控制调试功能是否开启,如下:
#include "stdio.h"
static int g_debugFlag = false; // 调试开关
void PrintfFun(char *str) // 调试代码具体实现
{
if (g_debugFlag) {
printf("This is the %s running: \n", str);
}
}
int main(int argc, const char *argv[])
{
PrintfFun("Hello MyDebug"); // 使用实例
system("pause");
return 0;
}
本文提供一中使用宏定义来控制调试信息是否打开的方式:
#include "stdio.h"
static int g_debugFlag = false; // 调试开关
void PrintfFun(char *str) // 调试代码具体实现
{
printf("This is the %s running: \n", str);
}
#define MyPrintf(str) if (g_debugFlag) { PrintfFun(str);} // 使用宏函数来控制调试功能是否开启。
int main(int argc, const char *argv[])
{
MyPrintf("Hello MyDebug"); // 使用实例
system("pause");
return 0;
}
使用宏函数将调试开关判断提前,减少函数调用的开销,提升整体性能。