不可混用使用不同版本 CRT 運行時函式庫的第三方函式庫。換言之,確保程式只有一個 CRT 運行時函式庫實體,即統一採用動態鏈結,並使用同一版本的 CRT 運行時函式庫。
如果考慮到發佈版本的簡易性,程式可能會採用靜態聯結的方式 (視政策而定)。與該程式搭配的動態函式庫,其輸出入皆使用原始型別 (primitive data type),如: char, int, float, char *。

使用

    • 開啟 Project 之後,先透過 File → Add → Existing Project 加入其它工程至當前 Solution。之後可透過於 Solution 按右鍵添加其它工程。

編程技巧

    • Files that are opened by fopen_s and _wfopen_s are not sharable. If you require that a file be sharable, use _fsopen, _wfsopen with the appropriate sharing mode constant—for example, _SH_DENYNO for read/write sharing.
    • fopen 無法和其它程序共享同一個檔案。須改用 _fsopen

除錯

  • Debug → Exceptions → Win32 Exceptions
    • 勾選 c0000005 Access violation
    • 及早於例外發生時中斷 debugger,比較好發現問題所在。
    • However, if the application is being debugged, the debugger sees all exceptions before the program does. This is the distinction between the first and second chance exception: the debugger gets the first chance to see the exception (hence the name). If the debugger allows the program execution to continue and does not handle the exception, the program will see the exception as usual. If the program does not handle the exception, the debugger gets a second chance to see the exception. In this latter case, the program normally would crash if the debugger were not present.
確認 .pdb 檔需要擺放的位置和欲除錯的 exe 需要擺放的目錄 (查看 *.xml 檔)。

運行時函式庫

    • Properties → Configuration Properties → C/C++ → Code Generation
    • /MT
      • Causes the application to use the multithread, static version of the run-time library.
      • 多執行緒、靜態鏈結。
    • /MD
      • Causes the application to use the multithread-specific and DLL-specific version of the run-time library.
      • 多執行緒、動態鏈結。
    • libcmt.lib: 靜態 C 運行時函式庫。
      • C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\lib
    • msvcp90.dll → VC9 → VS2008
      • C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT
    • msvcp80.dll → VC8 → VS2005
      • MSVCR*.DLL: 動態 C 運行時函式庫。
      • MSVCP*.DLL: 動態 C++ 運行時函式庫。
      • MSVCM*.DLL: 動態 C/C++ 運行時函式庫,供 .NET/Native 混合編程使用。
      • LIBCMT: 靜態 C 運行時函式庫。
    • 程式和其依賴的第三方函式庫最好能使用同一份 CRT 運行時函式庫實體。
    • 動態內存分配 (Dynamically Allocated Memory)、全域和靜態變數 (Global and Static Data)、結構和物件傳遞 (Passing C/C++ Objects)、內聯函式和樣板類別與函式 (In-Line Functions, Template Classes, and Template Functions)、檔案和輸出入操作 (File I/O and Operations)、環境變數 (Environment Variables)、系統資源 (Resources) 都和 CRT 運行時函式庫相關。
      • 動態內存分配
        • 不同 CRT 運行時實體管理各自的堆。所以在不同 CRT 運行時,呼叫 malloc/free 和 new/delete 會有問題。注意! 動態內存分配可能在背後執行,如: C++ vector 的內存分配。另外,不同 CRT 運行時對內存的解讀可能也會有所不同,或是存取時會造成各種錯誤。
      • 全域和靜態變量
        • 不同 CRT 運行時有各自的靜態變量。如: strtok 的實現有靜態變量,在不同 CRT 運行時調用 strtok 會有問題,因為其中的靜態變量不同步。
      • 結構和物件傳遞
        • C++ 物件其實現,有可能會使用到動態內存分配。另外,不同 CRT 運行時對內存的解讀可能也會有所不同,不論是傳值 (passed by value) 或是傳參考 (passed by reference)。
      • 內聯函式和樣板類別與函式
        • 內聯函式和樣板類別與函式,其實體化在不同 CRT 運行時會有不同。
      • 檔案和輸出入操作
        • 在不同 CRT 運行時,呼叫 fopen、fclose 和 freopen 會有問題。
        • C++ 的 iostream 是模板類別,其實體化在不同 CRT 運行時會有不同。
      • 環境變數
        • 每一個 CRT 運行時實體管理各自的環境變數,彼此之間不同步。在不同 CRT 運行時,呼叫 getenv 和 putenv 會有問題。
      • 系統資源

If a dynamic-link library (DLL) contains a static link to the run-time library, it has its own instance of the run-time heap, and therefore its own heap, independent of the application's local heap.

例外處理

    • Specifies the model of exception handling to be used by the compiler and destroys C\+\+ objects that will go out of scope as a result of the exception. If /EH is not specified, the compiler will catch structured and C\+\+ exceptions, but will not destroy C\+\+ objects that will go out of scope as a result of the exception.
      • 不指定 /EH 時,不做 unwinding。即針對棧上的物件,不會調用其解構子。
    • /EHsc
      • Use /EHs to specify the synchronous exception handling model (C\+\+ exception handling without structured exception handling exceptions).
      • If used with s (/EHsc), catches C\+\+ exceptions only and tells the compiler to assume that extern C functions never throw a C\+\+ exception.
    • /EHa
      • Use /EHa to specify the asynchronous exception handling model (C\+\+ exception handling with structured exception handling exceptions).
    • In previous versions of Visual C\+\+, the C\+\+ exception handling mechanism supported asynchronous (hardware) exceptions by default. Under the asynchronous model, the compiler assumes any instruction may generate an exception. With the new synchronous exception model, now the default, exceptions can be thrown only with a throw statement.
    • 在非同步例外模行中 (asynchronous exception model),編譯器必須假設每道指令皆可能發生例外。
    • 在同步例外模型中 (synchronous exception model),例外只能透過 throw 陳述式丟出。編譯器可以減少相關代碼產生。

優化

Profile

  • 先對目標程序做 Sampling。
  • 再針對目標程序有針對性的做 Instrument。若是針對全局插樁,則蒐集到的資訊有可能會塞爆硬盤。
  • 分析數據。
        • Functions with the Most Individual Work
          • A list of the functions that directly executed at the top of the call stack for the longest time. Time spent in child functions is not included.
          • 不考慮子函式花費時間,取在棧頂端花費時間最多的函式。
        • Functions Taking the Longest
          • A list of the functions that were executing on the call stack for the longest times. Time spent in calls to child functions is included.
          • 考慮子函式花費時間,取花費時間最多的函式。
    • Profiling by Using the Sampling Method
      • It is recommended to use a release build to detect performance problems in your application. A release build is recommended for profiling because a debug build has additional information compiled into it that may adversely affect performance and fail to illustrate performance issues accurately. A release build does not automatically give you symbol information. To configure your build so symbol information is visible in profiler performance reports, see /Z7, /Zi, /ZI (Debug Information Format) and How to: Reference Windows Symbol Information.
      • 目標程序需要有相應的 PDB 檔
    • Profiling by Using the Instrumentation Method
        • The executable that is to be profiled needs to have been linked with the /profile switch, which can be done with a simple recompile after changing the corresponding configuration option.
    • The first benefit of this feature is reduced instrumentation probe execution overhead.
    • The second benefit is reduced profile data file (.vsp) size.
    • With this feature enabled, it’s important to note that the time spent executing a small function does not just disappear.

命令行工具

PS C:\> DUMPBIN /ARCHIVEMEMBERS libboost_graph-vc80-mt-1_57.lib
PS C:\> LIB libboost_graph-vc80-mt-1_57.lib /EXTRACT:bin.v2\libs\graph\build\msvc-8.0\release\address-model-32\link-static\threading-multi\read_graphviz_new.obj
# 或是直接用 Notepad 開啟 read_graphviz_new.obj,搜尋 manifestdependency,後面的字串包含其鏈結 C 運行時函式庫的版本。
PS C:\> DUMPBIN /DIRECTIVES read_graphviz_new.obj

MSBuild

術語

其它

Q & A

外部連結

登录