WHAT'S NEW?
Loading...

[VS依賴庫管理]Boost動態連結


當你使用boost動態連結函式庫時,編譯時會出現以下的錯誤代碼嗎?
error C1189: #error :  "Mixing a dll boost library with a static runtime is a really bad idea..."

的確,boost自動連結功能(auto-linking)帶給我們便利性,節省專案設置的時間,然而如何正確設定專案就成為很重要課題。以上錯誤代碼C1189意思是『用靜態Runtime混合boost動態連結庫真的昰一個錯誤想法』,也就是說Boost動態連結的專案屬性設置錯誤。

設定Boost動態連結的專案屬性主要有以下的步驟:
  1. 在前置處理器,強制動態連結Boost函式庫
  2. 在Code Generation選擇Runtime Library
  3. 增加Boost標題檔和函式庫的路徑

錯誤代碼C1189就是在第二步Runtime Library設定錯誤,此步驟主要設定在多執行緒模式下如何連結C和C++執行函式庫(C and C++ Runtime Library),依據偵錯和發佈(Debug & Release)和靜態和動態函式庫(Static & Dll函式庫)主要分成以下四種:
  • Multi-threaded (/MT)
  • Multi-threaded Debug (/MTd)
  • Multi-threaded DLL (/MD)
  • Multi-threaded Debug DLL (/MDd)
簡單來說,若字尾帶有-d即偵錯版,沒有小寫d即發佈版。還有,判斷動態連結庫依據是多執行緒(Multi-threaded)後面是否有DLL,即縮寫為(/MD),而靜態連結則是直接為多執行緒縮寫(/MT)。

環境

  • Windows 7 64 bit
  • VS C++ 2015 
    • 方案:ThirdPartyExample
      • 專案名稱:TestBoost_Dynamic
      • 專案配置:Win32 Debug & Release
  • Boost  1.59.0
    • 下載教學:Boost簡介與分割
    • 所使用到動態連結程式庫(dll)
      • Boost.System
      • Boost.DateTime
      • Boost.Regex
  • 依賴庫相對路徑(環境變數)
    • 標頭檔路徑:$(TParty_DIR)\Boost\Include
    • 函式庫路徑:$(TParty_DIR)\Boost\Lib
    • 打包dll檔$(TParty_DIR)\bin

教程

在前置處理器(Preprocessor)新增定義

  1. 首先,在專案屬性頁面選擇C/C++』→『Preprocessor後,在Preprocessor Definitions 』加入以下的定義強制動態連結Boost函式庫
    1. BOOST_ALL_DYN_LINK
    圖一. 更改專案前置處理器

設定Runtime Library

  1. 首先,在專案屬性頁面選擇C/C++』→『Code Generation後,請根據專案組態選擇正確的Runtime Library,如以下所示。
    • 偵錯版(Debug): Multi-threaded Debug DLL (/MDd)
    • 發佈版(Release): Multi-threaded DLL (/MD)
    圖二. 選擇正確Runtime Library

增加Boost函式庫路徑

  1. 首先,在專案屬性頁面選擇C/C++』→『General後,找到Additional Include Directories』加入以下Boost標頭檔路徑
    1. $(TParty_DIR)\Boost\Include
    圖三. 加入Boost的標頭檔路徑

  2. 接下來,在同頁面下,點選Linker』在右頁面找到Additional Library Directories加入以下Boost函式庫路徑
    1. $(TParty_DIR)\Boost\Lib
    圖四. 加入Boost的函式庫路徑


0 comments:

張貼留言