在C/C++的案例中经常使用的Debug技巧之一,就是使用前处理器 #define #ifdef #endif 来夹住Debug程式码。
程序开发阶段大量详细的捕捉可能出问題的情形并输出Debug信息或中断是非常重要的,但是这些处理”几乎不会发生的情形”的程式码当然会影响程序效能。
使用#ifdef就可以直接在编译时决定要生成release版本或是debug版本,非常的方便。
简单介绍一下c中是怎么用的。
一般情况下,源程序中所有的行都参加编译。但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是“条件编译”。有时,希望当满足某条件时对一组语句进行编译,而当条件不满足时则编译另一组语句。
拿debug举例:
#ifdef DEBUG
print ("device_open(%p)\n", file);
#endif
如果在它的前面有以下命令行:
#define DEBUG
则在程序运行时输出file指针的值,以便调试分析。调试完成后只需将这个define命令行删除即可。有人可能觉得不用条件编译也可达此目的,即在调试时加一批printf语句,调试后一一将printf语句删除去。的确,这是可以的。但是,当调试时加的printf语句比较多时,修改的工作量是很大的。用条件编译,则不必一一删改printf语句,只需删除前面的一条“#define DEBUG”命令即可,这时所有的用DEBUG作标识符的条件编译段都使其中的printf语句不起作用,即起统一控制的作用,如同一个“开关”一样。
Java这种常用在大型案例的语言沒有类似的功能,不过要模仿#ifdef的功能是可以的。简单说,Java 编译器为了最佳化,只要发现条件式的值为false,编译时就会把该区段代码忽略。所以只要定有一个 public final boolean 变量就可以了。例如:
public interface Debug
{
public final boolean ENABLE = true;
}
public class Hello
{
if(Debug.ENABLE)
{
System.out.println("This is debug message.");
}
}
分享到:
相关推荐
#ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif 已测试,能正常运行。 //地图状态 #define MAP_BACKGROUP 48 //'0' 对应字符'0'背景 #define MAP_WHITEWALL 49 /...
// #define DEBUG(VAR) mylog.Print(__FILE__, __LINE__, VAR, LOG_DEBUG) // // #else // // #define DEBUG(VAR) function //项目用的标准的日志函数. // // #endif // // // //Plan :1.0版本写成可以带参数的日志...
商品销售管理系统C++ SQL2000. #include "stdafx.h" // Standard windows header file #include "BCMenu.h" // BCMenu class declaration #include <afxpriv.h> //SK: makes A2W and other spiffy AFX macros work ...
# define TT_DEBUG printf("<%s:%d>", __func__, __LINE__),printf # else # define TT_DEBUG # endif //__TIME_TRACE_INER_DEBUG__ #else // __TIME_TRACE_DEBUG__ # define TIME_TRACE_RESET # define ...
#define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CChildFrame IMPLEMENT_DYNCREATE...
代码如下:#include “stdafx.h” #include<stdlib> #include<crtdbg> #include <iostream> // 内存泄露检测机制 #define _CRTDBG_MAP_ALLOC #ifdef _DEBUG #define new new(_NORMAL_BLOCK, __FILE_
最近在看《Linux C编程一站式学习》这本书,就想到一个方法: 代码如下:void myprintf(char* fmt, …){}#ifdef DEBUG#define printf(fmt, args…) myprintf(fmt, ##args)#endif调试阶段带着DEBUG调试,正式上线就...
#define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CRgn rgn; ///////////////////////////////////////////////////////////////////////////// // CMainFrame int SCR_Width = ...
#define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif HCURSOR m_hcursor; ///////////////////////////////////////////////////////////////////////////// // CMainFrame ...
#define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CNewStatic CNewStatic::CNewStatic()...
要使本例程序代码在Release下通过,需要在PlayerDlg.cpp的声明中做如下修改: Debug状态下的代码: #ifdef _DEBUG #define new DEBUG_NEW #define WM_MYMESSAGE WM_USER+6 #undef THIS_FILE ...
代码如下:#ifdef _DEBUG#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)#else#define DEBUG_CLIENTBLOCK#endif#define _CRTDBG_MAP_ALLOC#include <crtdbg>#ifdef _DEBUG#define new DEBUG_...
#define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CLab_linkApp BEGIN_MESSAGE_MAP(CLab...
#define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMyCalrrApp BEGIN_MESSAGE_MAP...
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__) #else #define DEBUG_CLIENTBLOCK #endif #define _CRTDBG_MAP_ALLOC #include #ifdef _DEBUG #define new DEBUG_CLIENTBLOCK #...
简易视频播放器已通过C++6.0编译,但播放的格式较少。 // vc6_MediaPlayerDlg.cpp : implementation file // #include "stdafx.h" #include "vc6_MediaPlayer.h" #include "vc6_MediaPlayerDlg.h" #ifdef _DEBUG #...
#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: //{{AFX_MSG(CHomework2Doc) // NOTE - ...
#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // control bar embedded members CStatusBar m_wndStatusBar; CToolBar m_wndToolBar; // ...
#define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About ...
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define GAP 1 #ifndef OBM_CHECK #define OBM_CHECK 32760 // from winuser.h #endif #if _MFC_VER #error ...