VCä¸DebugåReleaseåºå«
æè¿å代ç è¿ç¨ä¸ï¼åç° Debug ä¸è¿è¡æ£å¸¸ï¼Release ä¸å°±ä¼åºç°é®é¢ï¼ç¾æä¸å¾å
¶è§£ï¼èRelease
ä¸åæ æ³è¿è¡è°è¯ï¼äºæ¯åªè½éç¨printfæ¹å¼éæ¥å®ä½å°é®é¢æå¨å¤ï¼æåç°åæ¥æ¯ç»å®çä¸ä¸ªæ°ç»æªåå§åï¼å¯¼è´åé¢å¤çå¼å¸¸ãç½ä¸æ¥æ¾äºäºèµæï¼å¨è¿
ç½åæ±æ»ä¸ï¼å为å¤å¿ï½
ä¸ãDebug å Release çåºå«
Debug é常称为è°è¯çæ¬ï¼å®å
å«è°è¯ä¿¡æ¯ï¼å¹¶ä¸ä¸ä½ä»»ä½ä¼åï¼ä¾¿äºç¨åºåè°è¯ç¨åºãRelease 称为åå¸çæ¬ï¼å®å¾å¾æ¯è¿è¡äºåç§ä¼åï¼ä½¿å¾ç¨åºå¨ä»£ç 大å°åè¿è¡é度ä¸é½æ¯æä¼çï¼ä»¥ä¾¿ç¨æ·å¾å¥½å°ä½¿ç¨ã
Debug å Release ççæ£åºå«ï¼å¨äºä¸ç»ç¼è¯é项ã
Debug çæ¬
åæ° å«ä¹
/MDd /MLd æ /MTd ä½¿ç¨ Debug runtime library(è°è¯çæ¬çè¿è¡æ¶å»å½æ°åº)
/Od å
³éä¼åå¼å
³
/D "_DEBUG" ç¸å½äº #define _DEBUG,æå¼ç¼è¯è°è¯ä»£ç å¼å
³(主è¦é对assertå½æ°)
/ZI
å建 Edit and continue(ç¼è¾ç»§ç»)æ°æ®åºï¼è¿æ ·å¨è°è¯è¿ç¨ä¸å¦æä¿®æ¹äºæºä»£ç ä¸ééæ°ç¼è¯
GZ å¯ä»¥å¸®å©æè·å
åé误
Release çæ¬ åæ°å«ä¹
/MD /ML æ /MT 使ç¨åå¸çæ¬çè¿è¡æ¶å»å½æ°åº
/O1 æ /O2 ä¼åå¼å
³ï¼ä½¿ç¨åºæå°ææå¿«
/D "NDEBUG" å
³éæ¡ä»¶ç¼è¯è°è¯ä»£ç å¼å
³(å³ä¸ç¼è¯assertå½æ°)
/GF å并éå¤çå符串ï¼å¹¶å°å符串常éæ¾å°åªè¯»å
åï¼é²æ¢è¢«ä¿®æ¹
Debug å Release 并没ææ¬è´¨ççéï¼ä»ä»¬åªæ¯ä¸ç»ç¼è¯é项çéåï¼ç¼è¯å¨åªæ¯æç
§é¢å®çé项è¡å¨ã
1. åéã
大家é½ç¥éï¼debugè·releaseå¨åå§ååéæ¶æåçæä½æ¯ä¸åçï¼debugæ¯å°æ¯ä¸ªåèä½é½èµæ0xcc(注1)ï¼èreleaseçèµå¼è¿
ä¼¼äºéæº(ææ³æ¯ç´æ¥ä»å
åä¸åé
çï¼æ²¡æåå§åè¿)ãè¿æ ·å°±æç¡®äºï¼å¦æä½ çç¨åºä¸çæ个åé没被åå§å就被å¼ç¨ï¼å°±å¾æå¯è½åºç°å¼å¸¸ï¼ç¨ä½æ§å¶åéå°
导è´æµç¨å¯¼åä¸ä¸è´ï¼ç¨ä½æ°ç»ä¸æ å°ä¼ä½¿ç¨åºå´©æºï¼æ´å å¯è½æ¯é æå
¶ä»åéçä¸åç¡®èå¼èµ·å
¶ä»çé误ãæ以å¨å£°æåéå马ä¸å¯¹å
¶åå§åä¸ä¸ªé»è®¤çå¼æ¯æç®
åææçåæ³ï¼å¦å项ç®å¤§äºä½ æ¾é½æ²¡å°æ¹æ¾ã代ç åå¨é误å¨debugæ¹å¼ä¸å¯è½ä¼å¿½ç¥èä¸è¢«å¯è§å°ï¼å¦debugæ¹å¼ä¸æ°ç»è¶çä¹å¤§å¤ä¸ä¼åºéï¼å¨
releaseä¸å°±æ´é²åºæ¥äºï¼è¿ä¸ªæ¾èµ·æ¥å°±æ¯è¾é¾äº:( è¿æ¯èªå·±å¤å 注æå§
åµåµï¼å°±æ¯æç¯çé®é¢ï½ï½
2. èªå®ä¹æ¶æ¯çæ¶æ¯åæ°ã
MFC为æ们æä¾äºå¾å¥½çæ¶æ¯æºå¶ï¼æ´å¢å äºèªå®ä¹æ¶æ¯ï¼å¥½å¤æå°±ä¸ç¨å¤è¯´äºãè¿ä¹åå¨debugè·releaseçé®é¢åï¼çæ¡æ¯è¯å®çãå¨èªå®ä¹æ¶æ¯
çå½æ°ä½å£°ææ¶ï¼æ¶å¸¸ä¼çå°è¿æ ·çåæ³ï¼afx_msg LRESULT OnMessageOwn();
Debugæ
åµä¸ä¸è¬ä¸ä¼æä»»ä½é®é¢ï¼èå½ä½ å¨Releaseä¸ä¸å¤çº¿ç¨æè¿ç¨é´ä½¿ç¨äºæ¶æ¯ä¼ éæ¶å°±ä¼å¯¼è´æ æå¥æä¹ç±»çé误ã导è´è¿ä¸ªé误ç´æ¥åå æ¯æ¶
æ¯ä½çåæ°æ²¡ææ·»å ï¼å³åºè¯¥åæï¼afx_msg LRESULT OnMessageOwn(WPARAM wparam, LPARAM
lparam); (注2)
3. release模å¼ä¸ä¸åºéï¼ä½debug模å¼ä¸æ¥éã
è¿ç§æ
åµä¸å¤§å¤ä¹æ¯å 为代ç 书åä¸æ£ç¡®å¼èµ·çï¼æ¥çMFCçæºç ï¼å¯ä»¥åç°å¥½å¤ASSERTçè¯å¥(æè¨)ï¼è¿ä¸ªå®åªæ¯å¨debug模å¼ä¸æææï¼é£ä¹å°±
æ¸
æ¥äºï¼releaseçä¸æ¥éæ¯å¿½ç¥äºé误èä¸æ¯æ²¡æé误ï¼è¿å¯è½åå¨å¾å¤§çéæ£ï¼å 为æ¯Debug模å¼ä¸ï¼æ¯è¾æ¹ä¾¿è°è¯ï¼å¥½å¥½çæ£æ¥èªå·±ç代ç ï¼åæ¤
å°±ä¸å¤è¯´äºã
4. ASSERT, VERIFY, TRACE..........è°è¯å®
è¿ç§æ
åµå¾å®¹æ解éã举个ä¾åï¼è¯·å¨VCä¸è¾å
¥ASSERTç¶åéä¸æF12è·³å°å®å®ä¹çå°æ¹ï¼è¿éä½ å°±è½å¤åç°Debugä¸ASSERTè¦æ§è¡
AfxAssertFailedLineï¼èReleaseä¸çå®å®ä¹å´ä¸º"#define ASSERT(f)
((void)0)"ãæ以注æå¨è¿äºè°è¯å®çè¯å¥ä¸è¦ç¨ç¨åºç¸å
³åéå¦i++åæä½çè¯å¥ãVERIFYæ¯ä¸ªä¾å¤ï¼"#define
VERIFY(f) ((void)(f))"ï¼å³æ§è¡ï¼è¿éçä½ç¨å°±ä¸å¤è¿½ç©¶äºï¼æå
´è¶£å¯èªå·±ç 究:)ã
æ»ç»ï¼
Debugä¸Releaseä¸åçé®é¢å¨åå¼å§ç¼å代ç æ¶ä¼ç»å¸¸åçï¼99%æ¯å ä¸ºä½ ç代ç 书åé误è导è´çï¼æ以ä¸è¦å¨ä¸å¨å°±è¯´ç³»ç»é®é¢æç¼è¯å¨é®é¢ï¼
åªåæ¾æ¾èªå·±çåå ææ¯æ ¹æ¬ãæä»å就常常éå°è¿æ
åµï¼ç»åè¿ä¸æ¬¡æ¬¡çæè®åæå°±å¼å§æ³¨æäºï¼ç°å¨ææåè¿ç代ç æå·²ç»å¥½ä¹
没éå°è¿ç§é®é¢äºãä¸é¢æ¯å 个
é¿å
çæ¹é¢ï¼å³ä½¿æ²¡æè¿ç§é®é¢ä¹åºæ³¨æä¸ä¸ï¼
1. 注æåéçåå§åï¼å°¤å
¶æ¯æéåéï¼æ°ç»åéçåå§å(å¾å¤§çæ
åµä¸å¦ä½èèäº)ã
2. èªå®ä¹æ¶æ¯åå
¶ä»å£°æçæ ååæ³
3. 使ç¨è°è¯å®æ¶ä½¿ç¨åæ好注éæ
4. å°½é使ç¨try - catch(...)
5. å°½é使ç¨æ¨¡åï¼ä¸ä½è¡¨è¾¾æ¸
æ¥èä¸æ¹ä¾¿è°è¯ã
注1ï¼
debugçåå§åæ0xccæ¯å 为0xccå¨x86ä¸æ¯ä¸æ¡int 3åæ¥ä¸ææ令ï¼è¿æ ·ç¨åºå¦æè·é£äºéå°0xccå°±ä¼åä¸æ¥ï¼è¿ååçæºç¼ç¨æ¶ä¸è¬å°æ²¡ç¨ç代ç 空é´å¡«å
¥jmp 0000è¯å¥æ¯ä¸æ ·å°è½¬è´´äºï¼è®¡ç®æºäºçº§èè¯_èè¯å¤§ãè´£ç¼:drfcy çº éã
[VC]DEBUGåRELEASE2007å¹´08æ26æ¥ æææ¥ ä¸å 04:33 I. å
ååé
é®é¢
1. åéæªåå§åãä¸é¢çç¨åºå¨debugä¸è¿è¡çå¾å¥½ã
thing * search(thing * something)
BOOL found;
for(int i = 0; i < whatever.GetSize(); i++)
{
if(whatever[i]->field == something->field)
{ /* found it */
found = TRUE;
break;
} /* found it */
}
if(found)
return whatever[i];
else
return NULL;
èå¨releaseä¸å´ä¸è¡ï¼å 为debugä¸ä¼èªå¨ç»åéåå§åfound=FALSE,èå¨releaseçä¸åä¸ä¼ãæ以尽å¯è½çç»åéãç±»æç»æåå§åã
2. æ°æ®æº¢åºçé®é¢
å¦ï¼char buffer[10];
int counter;
lstrcpy(buffer, "abcdefghik");
å¨debugçä¸bufferçNULLè¦çäºcounterçé«ä½ï¼ä½æ¯é¤écounter>16M,ä»ä¹é®é¢ä¹æ²¡æãä½æ¯å¨releaseç
ä¸ï¼counterå¯è½è¢«æ¾å¨å¯åå¨ä¸ï¼è¿æ ·NULLå°±è¦çäºbufferä¸é¢ç空é´ï¼å¯è½å°±æ¯å½æ°çè¿åå°åï¼è¿å°å¯¼è´ACCESS
ERRORã
3. DEBUGçåRELEASEççå
ååé
æ¹å¼æ¯ä¸åç ãå¦æä½ å¨DEBUGçä¸ç³è¯· ele 为
6*sizeof(DWORD)=24bytes,å®é
ä¸åé
ç»ä½ çæ¯32bytesï¼debugç以32bytes为åä½åé
ï¼ï¼
èå¨releaseçï¼åé
ç»ä½ çå°±æ¯24bytesï¼releaseç以8bytes为åä½ï¼ï¼æ以å¨debugçä¸å¦æä½ åele[6],å¯è½ä¸ä¼æ
ä»ä¹é®é¢ï¼èå¨releaseçä¸ï¼å°±æACCESS VIOLATEã
II. ASSERTåVERIFY
1. ASSERTå¨Releaseçæ¬ä¸æ¯ä¸ä¼è¢«ç¼è¯çã
ASSERTå®æ¯è¿æ ·å®ä¹ç
#ifdef _DEBUG
#define ASSERT(x) if( (x) == 0) report_assert_failure()
#else
#define ASSERT(x)
#endif
å®é
ä¸å¤æä¸äºï¼ä½æ å
³ç´§è¦ãåå¦ä½ å¨è¿äºè¯å¥ä¸å äºç¨åºä¸å¿
é¡»è¦æç代ç
æ¯å¦
ASSERT(pNewObj = new CMyClass);
pNewObj->MyFunction();
è¿ç§æ¶åReleaseçæ¬ä¸çpNewObjä¸ä¼åé
å°ç©ºé´
æ以æ§è¡å°ä¸ä¸ä¸ªè¯å¥çæ¶åç¨åºä¼æ¥è¯¥ç¨åºæ§è¡äºéæ³æä½çé误ãè¿æ¶å¯ä»¥ç¨VERIFY ï¼
#ifdef _DEBUG
#define VERIFY(x) if( (x) == 0) report_assert_failure()
#else
#define VERIFY(x) (x)
#endif
è¿æ ·çè¯ï¼ä»£ç å¨releaseçä¸å°±å¯ä»¥æ§è¡äºã
III. åæ°é®é¢ï¼
èªå®ä¹æ¶æ¯çå¤çå½æ°ï¼å¿
é¡»å®ä¹å¦ä¸ï¼
afx_msg LRESULT OnMyMessage(WPARAM, LPARAM);
è¿åå¼å¿
é¡»æ¯HRESULTåï¼å¦åDebugä¼è¿ï¼èReleaseåºé
IV. å
ååé
ä¿è¯æ°æ®å建åæ¸
é¤çç»ä¸æ§ï¼å¦æä¸ä¸ªDLLæä¾ä¸ä¸ªè½å¤å建æ°æ®çå½æ°ï¼é£ä¹è¿ä¸ªDLLåæ¶åºè¯¥æä¾ä¸ä¸ªå½æ°éæ¯è¿äºæ°æ®ãæ°æ®çå建åæ¸
é¤åºè¯¥å¨åä¸ä¸ªå±æ¬¡ä¸ã
V. DLLçç¾é¾
人们å°ä¸åçæ¬DLLæ··åé æçä¸ä¸è´æ§å½¢è±¡ç称为 âå¨æè¿æ¥åºçå°ç±â(DLL Hell) ï¼çè³å¾®è½¯èªå·±ä¹è¿ä¹è¯´
http://msdn.microsoft.com/library/techart/dlldanger1.htm)ã
å¦æä½ çç¨åºä½¿ç¨ä½ èªå·±çDLLæ¶è¯·æ³¨æï¼
1. ä¸è½å°debugåreleaseççDLLæ··åå¨ä¸èµ·ä½¿ç¨ãdebugé½æ¯debugçï¼releaseçé½æ¯releaseçã
解å³åæ³æ¯å°debugåreleaseçç¨åºåå«æ¾å¨ä¸»ç¨åºçdebugåreleaseç®å½ä¸
2. åä¸ä¸è¦ä»¥ä¸ºéæè¿æ¥åºä¼è§£å³é®é¢ï¼é£åªä¼ä½¿æ
åµæ´ç³ç³ã
VI. RELEASEæ¿ä¸çè°è¯ ï¼
1. å°ASSERT() æ¹ä¸º VERIFY() ãæ¾åºå®ä¹å¨"#ifdef
_DEBUG"ä¸ç代ç ï¼å¦æå¨RELEASEçæ¬ä¸éè¦è¿äºä»£ç 请å°ä»ä»¬ç§»å°å®ä¹å¤ãæ¥æ¾TRACE(...)ä¸ä»£ç ï¼å 为è¿äºä»£ç å¨RELEASEä¸
ä¹ä¸è¢«ç¼è¯ã 请认çæ£æ¥é£äºå¨RELEASEä¸éè¦ç代ç æ¯å¦å¹¶æ²¡æ被便å®ã
2. åéçåå§åæ带æ¥çä¸åï¼å¨ä¸åçç³»ç»ï¼ææ¯å¨DEBUG/RELEASEçæ¬é´é½åå¨è¿æ ·çå·®å¼ï¼æ以请对åéè¿è¡åå§åã
3. æ¯å¦å¨ç¼è¯æ¶å·²ç»æäºè¦å?请å°è¦å级å«è®¾ç½®ä¸º3æ4,ç¶åä¿è¯å¨ç¼è¯æ¶æ²¡æè¦ååºç°.
VII. å°Project Settings" ä¸ "C++/C " 项ç®ä¸ä¼åé项æ¹ä¸ºDisbaleï¼Debugï¼ãç¼è¯å¨çä¼åå¯è½å¯¼è´è®¸å¤ææ³ä¸å°çé误ï¼è¯·å
http://www.pgh.net/~newcomer/debug_release.htm 1. æ¤å¤å¯¹RELEASEçæ¬ç软件ä¹å¯ä»¥è¿è¡è°è¯ï¼è¯·åå¦ä¸æ¹å¨ï¼
å¨"Project Settings" ä¸ "C++/C " 项ç®ä¸è®¾ç½® "category"
为 "General" 并ä¸å°"Debug Info"设置为 "Program Database"ã
å¨"Link"项ç®ä¸éä¸"Generate Debug Info"æ£æ¥æ¡ã
"Rebuild All"
å¦æ¤åæ³ä¼äº§ççä¸äºéå¶ï¼
æ æ³è·å¾å¨MFC DLLä¸çåéçå¼ã
å¿
须对该软件æ使ç¨çææDLLå·¥ç¨é½è¿è¡æ¹å¨ã
å¦ï¼
MS BUGï¼MSçä¸ä»½ææ¯ææ¡£ä¸è¡¨æï¼å¨VC5ä¸å¯¹äºDLLç"Maximize Speed"ä¼åé项并æªè¢«å®å
¨æ¯æï¼å æ¤è¿å°ä¼å¼èµ·å
åé误并导è´ç¨åºå´©æºã
2. æ
ä¸ä¸ªç¨åºDebugViewï¼ç¨æ¥ææOutputDebugStringçè¾åºï¼è¿è¡èµ·æ¥åï¼ä¼°è®¡æ¯èªè®¾ä¸ºsystem
debuggerï¼å°±å¯ä»¥è§çææç¨åºçOutputDebugStringçè¾åºãæ¤åï¼ä½ å¯ä»¥è±ç¦»VCæ¥è¿è¡ä½ çç¨åºå¹¶è§çè°è¯ä¿¡æ¯ã
3. æä¸ä¸ªå«Gimpel Lintçéæ代ç æ£æ¥å·¥å
· ä¸è¿è¦å$çã