編譯環境:WIN8.1,VS2013專業版update3
一、編譯boost本部落格已有,參考清單,不再贅述。
二、STLport适配 (直接儲存步驟二中修改後的源碼即可)
下載下傳stlport. 下載下傳位址:http://sourceforge.net/projects/stlport/
解壓到一個目錄下面, 我的是解壓到 G:\STLport_5_2_1
因為STLport并不完整支援C++11,是以需要修改一些STLport的源碼,如下:
1. 将_cstdlib.h(158)代碼修正為
#if !defined(_STLP_MSVC) || (_STLP_MSVC < 1600)
inline
_STLP_LONG_LONG
abs
(_STLP_LONG_LONG __x) {
return
__x < 0 ? -__x : __x; }
#endif
2. 将_cmath.h(497,498)的代碼包裹一層宏處理
#if !defined(_STLP_MSVC) || (_STLP_MSVC < 1800) // 修改後的 _cmath.h line 497
_STLP_MATH_INLINE2XX(float, hypot, hypot) //修改前的 _cmath.h line 497
inline long double hypot(long double x, long double y) { return sqrt(x * x + y * y); } //修改前的 _cmath.h line 498
#endif // 修改後的 _cmath.h line 500
3. 處理C++11的nullptr
編譯時出現"'nullptr_t':is not a member of 'stlpd_std'"
當我們在用STLport聯合編譯boost時,編譯過程會報出如上所示的錯誤,這個問題的原因在于,stlport不支援C++11标準中新增的nullptr_t和nullprt關鍵字,是以會出現這個錯誤,從網上查找到的解決辦法是:
在X:\STLport-5.2.1\stlport\cstddef檔案中增加下面一段代碼即可,(筆者寫在了文本作者注釋後的最前面)
#ifdef __cplusplus
namespace std{typedef decltype(__nullptr)nullptr_t;}
using ::std::nullptr_t;
#endif
4. 接着修改 "stlport\stl\type_traits.h" 檔案,在 66 行附近找到下面的代碼:
[cpp] view plain copy
- #ifdef _STLP_USE_BOOST_SUPPORT
- # include <stl/boost_type_traits.h>
- # include <boost/type_traits/add_reference.hpp>
- # include <boost/type_traits/add_const.hpp>
- #endif
在 #endif 前加入如下一行代碼:
[cpp] view plain copy
- # include <boost/type_traits/remove_const.hpp>
5. boost支援STLport,是以我們在用這兩個庫聯合程式設計時,在代碼最前面加入宏開關
#define _STLP_USE_BOOST_SUPPORT
加入後,比如我們就可以在STLport中使用更高效的boost::shared_ptr等高效元件。(#include <memeory>)
三、編譯STLport
1. 打開vs2013的指令行工具, 在目錄:C:\Program Files\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts\ 下面, <VS2013 x86 本機工具指令提示> 即是, 輕按兩下運作.
2. 然後,啟動 Visual Studio 2010 指令行方式,并定位到 STLport 主目錄下,并按如下步驟執行指令(注意替換 %boostDir% 為具體的 Boost 路徑):
[plain] view plain copy
- configure msvc9 (--with-static-rtl 可選的,看下表) (--with-dynamic-rtl 可選的,看下表) (--use-boost %boostDir% 如果有boost可選)
- //之後
- cd build\lib
- nmake -f msvc.mak clean install
--use-boost 指明 boost 庫的路徑。僅使用一下 boost 的幾個頭檔案,是以無需事先編譯 boost。
上面指令行的執行過程如下:
輸入configure msvc9, 如下圖:(STLport暫不支援MSVC10,11,12等高版本)
再定位目錄到 build/lib 下, 輸入nmake -f msvc.mak clean install 。編譯一會兒, 等個幾分鐘就編譯完了. 看一下\STLport-5.2.1目錄下面, 多了bin 和lib 兩個目錄. 裡面有dll 和 lib. 将dll複制到VC\bin目錄下面, 在項目中添加include, lib目錄即可使用stlport。
另外mark一下, 我是在win8.1 + vs2013環境下面使用的stlport, 它與C++ 11 的一些庫有沖突. 使用C++11的新加一些像thread , 項目就會報錯, stlport庫就不能使用. 總而言之, 權衡一下吧.
最後(STLport部分編譯參數)附表一張:
版本說明 | 生成的檔案 | 編譯說明 | 使用說明 |
使用動态運作史庫的動态庫版本 | stlport.5.2.dll stlport.5.2.lib stlportd.5.2.dll stlportd.5.2.lib stlportstld.5.2.dll stlportstld.5.2.lib | 預設會生成 | stlportstld.5.2.lib是調試模式版本,使用宏_STLP_DEBUG控制 |
使用靜态運作時庫的靜态庫版本 | stlport_static.lib stlportd_static.lib stlportstld_static.lib | 預設會生成 | stlportstld_static.lib是調試版本,使用宏_STLP_DEBUG控制 |
使用靜态運作時庫的動态庫版本 | stlportstld_x.5.2.lib stlportstld_x.5.2.dll stlportd_x.5.2.lib stlportd_x.5.2.dll stlport_x.5.2.lib stlport_x.5.2.dll | 配置時要 configure.bat msvc9 --with-static-rtl | 要修改 stlport/stl/config/host.h 增加#define _STLP_USE_DYNAMIC_LIB 調試版本需要宏_STLP_DEBUG |
使用動态運作時庫的靜态庫版本 | stlportstld_statix.lib stlportd_statix.lib stlport_statix.lib | 配置時要 configure.bat msvc9 --with-dynamic-rtl | 要修改 stlport/stl/config/host.h 增加#define _STLP_USE_STATIC_LIB 調試版本需要宏_STLP_DEBUG |
至此,boost和STLport的高效代碼的美好時光就開始了,讓我們抛棄VC自帶的低效庫吧。不過個人真心喜歡VS的文本編輯界面,哈,是以不舍得删。