`
anlx27
  • 浏览: 491400 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

#ifdef DEBUG;debug();#endif

阅读更多

 

参考地址:http://www.cnblogs.com/ezrax/archive/2008/08/06/1262032.html

 

在看例程的时候,经常看到主函数开始前会有这样的代码,知道是用作调试的。但对此比较模糊,

 

int main(void)
{
#ifdef DEBUG
  debug();
#endif


   在工程设置里有一些设置会对该工程自动产生一系列的宏,用以控制程序的编译和运行。如果你把代码夹在#ifdef DEBUG 和对应的 #endif 中间,那么这段代码只有在调试(DEBUG)下才会被编译。也就是说,如果你在RELEASE模式下,这些代码根本就不会存在于你的最终代码里头

   你可以手动实现DEBUG的定义如:
   
   #define DEBUG

  这些宏代码本身是面向编译器使用的,不要用来实现你的业务逻辑代码,这样会带来很不好的影响。这类宏定义的一个典型应用就是产生/屏蔽调试信息,看下面的例子

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1 #include <iostream>
 2 using namespace std;
 3 
 4 #ifdef DEBUG
 5     inline void msg(){ cout<<"I'm testing"; }
 6 #else
 7     inline void msg() {}
 8 #endif
 9 
10 int main()
11 {
12     msg();
13 
14     return 0;
15 }


   上面的代码在DEBUG 模式下是会输出一句话的,你可以把这种应用放在你希望对程序的执行过程进行跟踪的代码里,为它们加上一些‘痕迹’,方便你进行判断。而在其他模式(RELEASE)下,这个函数只不过是一个空函数,又因为它是inline的,所以它实际上不会为你产生任何代码。这样你就能为自己的代码带来一种非常实用的效果,既能在DEBUG模式下为你提供判断的依据,又能在最终的RELEASE版中方便的抛弃掉它们,而不需要你手动地删除掉那些代码。

   作为宏指令,
如果定义了DEBUG宏那么编译#IF后面到#ENDIF的代码否则不编译DEBUG宏一般是编译器产生,如果编译模式是DEBUG就会产生这个宏,如果是RELEASE就不会

分享到:
评论

相关推荐

    iOS屏幕刷新率FPS计算器源码.zip

    #ifdef DEBUG #import "TXFPSCalculator.h" #endif STEP 2: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self.window makeKeyAndVisible...

    STM32单片机控制的高压无刷直流电机程序源代码.rar

    #ifdef DEBUG debug(); #endif /*Core initialization*/ /*NVIC first,before RCC initialization,use HSI,because RCC initialization also may have INT,GPIO last*/ NVIC_Configuration(); RCC_...

    C++中基于Crt的内存泄露检测

    #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__) #else #define DEBUG_CLIENTBLOCK #endif #define _CRTDBG_MAP_ALLOC #include #ifdef _DEBUG #define new DEBUG_...

    C++中关于Crt的内存泄漏检测的分析介绍

    代码如下:#ifdef _DEBUG#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)#else#define DEBUG_CLIENTBLOCK#endif#define _CRTDBG_MAP_ALLOC#include &lt;crtdbg&gt;#ifdef _DEBUG#define new DEBUG_...

    商品销售管理系统

    #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define BCMENU_GAP 1 #ifndef OBM_CHECK #define OBM_CHECK 32760 // from winuser.h #endif #if _MFC_VER ...

    Libcurl---编译好的lib文件,含release,debug版本;

    #ifdef _DEBUG #pragma comment(lib,"libcurld.lib") #else #pragma comment(lib,"libcurl.lib") #endif #pragma comment ( lib, "ws2_32.lib" ) #pragma comment ( lib, "winmm.lib" ) #pragma comment ( lib, ...

    8192CU LINUX驱动

    //#define CONFIG_DEBUG_CFG80211 1 #endif /* * Internal General Config */ //#define CONFIG_PWRCTRL //#define CONFIG_H2CLBK #define CONFIG_EMBEDDED_FWIMG 1 //#define CONFIG_FILE_FWIMG #ifdef ...

    stm32的can现场总线实验

    #ifdef DEBUG debug(); #endif /* System Clocks Configuration */ RCC_Configuration(); /* NVIC Configuration */ NVIC_Configuration(); /* GPIO ports pins Configuration */ GPIO_Configuration(); USART_...

    winzlog用VS2010重新编译的

    #ifdef _DEBUG #ifndef _WIN64 #pragma comment(lib, "zlog_vc100x86.debug.lib") #else #pragma comment(lib, "zlog_vc100x64.debug.lib") #endif #else #ifndef _WIN64 #pragma comment(lib, "zlog_vc100x86....

    超级终端程序

    #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif BEGIN_MESSAGE_MAP(CCSocketApp, CWinApp) ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ...

    STM32单片机实现PID算法,很实用,可以参考实际执行机构修改参数即可完成控制 软件源码.rar

    #ifdef DEBUG debug(); #endif System_Configuration(); printf("This is an example of PID!\n\r"); while (1) { switch (N){ case 0: if(Flag_1mS==1){ if(U_Set-ADCConvertedValue&gt;=10||U_Set-...

    使用C语言编写圣诞表白程序

    快到圣诞节啦,男票女票都有了嘛?没有的话,有没有喜欢的女孩子或者男孩子啊?今天就给你们分享一个圣诞节的表白程序,希望你们喜欢咯,效果是这样的: ...#ifdef _DEBUG #define new DEBUG_NEW #endif // 用

    linux 函数时间统计 打印

    #ifndef __TIME_TRACE_UTILS__ #define __TIME_TRACE_UTILS__ #include #include #include #include #include #include #include #include ...#define NS_TT_START namespace tt...#endif // __TIME_TRACE_DEBUG__

    圣诞节快到了,用C语言制作一个圣诞表白程序-供大家学习研究参考

    #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialogEx { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; ...

    STM32仿三菱PLC-源代码(含原理图).zip

    #ifdef DEBUG debug(); #endif //配置系统时钟 sys_config(); //配置 NVIC 和 Vector Table NVIC_Configuration(); //配置使用的GPIO口 GPIO_Configuration(); //改变指定管脚的映射 GPIO_Remap_SWJ_...

    AYYFPSIndicator简单的FPS指示

    #ifdef DEBUG // 1. 默认位置开启 FPS 监听,可拖动 [[AYYFPSIndicator sharedInstance] start]; #endif return YES; } 2. statusBar 显示 FPS值 - (BOOL)application:(UIApplication *)application ...

    VC环境MP3播放器的开发附实例源代码

     #ifdef _DEBUG  #define new DEBUG_NEW  #define WM_MYMESSAGE WM_USER+6  #undef THIS_FILE  static char THIS_FILE[] = __FILE__;  #endif  修改为Release状态下的代码:  #define WM_MYMESSAGE ...

    debug宏合集dbg.h

    #ifdef NDEBUG #define debug(M, ...) #else #define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) //牛逼 #endif #define clean_errno() (errno == 0 ? "None" : ...

    Http协议源码

    #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CHTTPClientApp

    redis是一个开源的、使用C语言编写的、支持网络交互的、可基于内存也可持久化的Key-Value数据库

    #ifdef _DEBUG #pragma comment(lib, "./lib/hiredisd.lib") #else #pragma comment(lib, "./lib/hiredis.lib") #endif #pragma warning(pop) // 恢复4200警告 更多问题可以到我的blog上留言

Global site tag (gtag.js) - Google Analytics