MSVC debug build

在VC++的設定:
C/C++若有定義_DEBUG 則需注意link應該link MTd or MDd
因為有些function 如_free_dbg是在定義_DEBUG時會被編譯(參考<crtdbg.h>)
此時如果link到release版的MT or MD,則會出現unresolved link

另外,這個錯誤訊息也是因為_DEBUG混用造成的

error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in ...

在<yvals.h≶(一些c++ std header會間接引用)

#ifdef _DEBUG
  #define _HAS_ITERATOR_DEBUGGING 1
#else
  #define _HAS_ITERATOR_DEBUGGING 0
#endif

#if _HAS_ITERATOR_DEBUGGING
  #define _ITERATOR_DEBUG_LEVEL 2
#elif _SECURE_SCL
  #define _ITERATOR_DEBUG_LEVEL 1
#else
  #define _ITERATOR_DEBUG_LEVEL 0
#endif

#ifdef __cplusplus
  //這邊檢查LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in ...
  #ifndef _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH
    #pragma detect_mismatch("_ITERATOR_DEBUG_LEVEL", _STRINGIZE(_ITERATOR_DEBUG_LEVEL))
  #endif /* _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH */
  //這邊檢查runtime lib:LNK2038 mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MTd_StaticDebug'
  #ifndef _ALLOW_RUNTIME_LIBRARY_MISMATCH
  //....
#endif /* __cplusplus */  

因此在整合各個不同的(static)library時,最簡單的方式是確保所有的編譯runtime選項是一致,對於c interface dll則不用特別關注編譯選項的問題,只要確保allocate resource和release resource和release都是在dll內部處理(例如不應該將dll內部malloc的resource 在dll外部free)

This entry was posted in Tips. Bookmark the permalink.

Leave a Reply