跳到主要內容

Setting Boost library in Qt for Windows 64-bits

Qt 是一個很優質的 C++ IDE,在前些日子 Qt Project release Qt 5.1.1 for Windows 64-bit (VS 2012, OpenGL, 522 MB) 的版本,於是重新安裝了這個版本。但是自己卻忘記之前 compile Boost 是 msvc 32-bits 的版本(Boost 的 compile 設定可以參考之前寫的 Building Wt with Visual Studio 2012 on Windows 8 x64 PC 這篇文章),在使用 Boost Getting Started on Windows 的 Regex source code 做測試時,卻出現 error LNK2019: unresolved external symbol錯誤,參考了 stackoverflow 的這篇文章,才知道用到 32-bits 的 regex Library ,整個修正的過程,紀錄在底下的文章,以後才不至於忘記。


Qt 5.1.1 for Windows 64-bit (VS 2012, OpenGL) 這個版本主要是使用 Visual Studio 2012 做為後端的 Compiler,然而安裝 VS 2012 Express for Desktop 可以使用 x64 or x86 的版本,所以安裝完  VS 2012 Express for Desktop 以後,再安裝 Qt 5.1.1 for Windows 64-bit (VS 2012, OpenGL) 就可以使用,並不需要做特別的設定。相同地,使用 Boost 也是一樣,只要不需要特別 link 到 Boost library 的程式,皆可以 compile 成功,並且可以執行。底下省略了安裝 VS 2012 及 Qt 的過程,並且使用之前 Boost 32-bits 的 library 來測試 Boost Getting Started on Windows 的 lambda 程式,整個過程為:

1. 在 Qt 中新增一個 Plain C++ Project:

   File > New File or Project > Non-Qt Project > Plain C++ Project

2. 將 main.cpp 的 source code 以 Boost lambda 程式替換:

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
     using namespace boost::lambda;
    typedef std::istream_iterator<int> in;
    std::for_each( in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
3. 在 .pro 中指定 Boost include and lib 位置:

# 在 .pro 中 comment 的符號是 # 
#INCLUDEPATH += D:/library/boost_154/include/boost-1_54
#LIBS += -LD:/library/boost_154/lib

INCLUDEPATH += C:/MinGW/include // 使用 STL 編譯的 Boost Library
LIBS += -LC:/MinGW/lib   // STL Library 是 for MinGW(.a library)

#unix|win32: LIBS += -lboost_regex-vc110-mt-gd-1_54
4. 執行 clean & qmake
  Build > clean all; Build > Run qmake
當每次修改 .pro 時,記得執行 clean all 以及 Run qmake 的動作,清空之前 Qt 系統所產生的暫存檔
5. 執行 Build and Run:

執行結果如下圖

但是對於需要 link 到 Boost library 的程式,例如 Boost 的 Regex source code,則必須使用 64-bits 的 Boost library 才能夠編譯成功,如果使用 32-bits 的 Boost library 會產生 unsolved external symbol 的錯誤訊息。問題是如何知道你 Boost library 是 32-bits or 64-bits 呢? 在 stackoverflow 的這篇文章中的 Answer 提到,可以使用 VS 2012 中的 dumpbin 程式來檢查,檢查的指令為:

dumpbin /headers libboost_regex-vc110-mt-gd-1_53.lib | findstr machine
於是使用 dumpbin 測試 d:\libraries\lib  Booost 的步驟:

1. 開啟 VS command line 畫面:
 Search > 輸入 VS > 點選 VS 2012 cross tools command
2. 測試 Boost library 的版本
 d:\ ; cd libraries\lib
 dumpbin /headers libboost_regex-vc110-mt-gd-1_53.lib | findstr machine
3. 結果顯示 x86 而不是 x64 :

確定了 Boost library 是 32-bits 以後,只好再次重新 compile Boost library,compile Boost 的步驟可以參考 Building Wt with Visual Studio 2012 on Windows 8 x64 PC 這篇文章,但是在執行 b2.exe 時要加入 address-model=64 的參數,編譯所產生的 Boost library 才是 for 64-bits 的 library。
再載了最新版的 boost 1.54 版本,在 VS 2012 command line 中執行下列指令:

.\b2 install --prefix=d:\library\boost_154 address-model=64 --build-type=complete

經過幾小時的編譯,將 Boost install 到 d:\library\boost_154 目錄中,可以再使用 dumpbin 程式來測試 compile 出來的 Boost 是不是 64-bits 版,結果是:


 確定 Boost library 是 64-bits 後,再次設定 Boost in Qt 的 include, lib 的路徑:

INCLUDEPATH += D:/library/boost_154/include/boost-1_54
LIBS += -LD:/library/boost_154/lib

#INCLUDEPATH += D:/libraries/include

#LIBS += -LD:/libraries/lib

#INCLUDEPATH += C:/MinGW/include
#LIBS += -LC:/MinGW/lib

unix|win32: LIBS += -lboost_regex-vc110-mt-gd-1_54

設定完成以後,再次執行 Clean All 及 Run qmake, compile Boost 的 Regex source code 程式就可以編譯成功。

留言

這個網誌中的熱門文章

CodeBlocks 多國語言的設定步驟

多年來一直都是使用 CodeBlocks 英文的介面,不曾想過要將 CodeBlocks 設定成多國語言的開發環境,對於不習慣於英文介面的國人,設定中文的使用介面是非常需要的環境,在 CodeBlocks 論壇有一篇文章提到 :Do you know http://wiki.codeblocks.org/index.php?title=Internationalization ?這個連結說明在 Windows 系統如何設定 CodeBlocks 成為 Internationalization 的環境,整個設定過程如下: 到 CodeBlocks 翻譯文件網站 下載 .mo 檔案:下載時需要 Ubuntu One 的帳號及密碼,登入後點選 .mo 檔案(不要下載 .po 檔是可編輯檔) 系統會傳送一封信件,點選信件的連結,將 .mo 檔案下載 將檔案複製到 C:\Codeblocks\share\CodeBlocks\locale\zh_TW 目錄(沒有這個目錄請自己建立) 開啟 CodeBlocks >> Setting >> Environment >> View >> Internationalization 選項打勾 >> 點選 Chinese 重新開啟 CodeBlocks 要加入其他語言的 .mo 檔案,則在 locale 目錄中新增其他語言的目錄名稱,例如: 德國 de_DE,這樣 CodeBlocks 就是多國語言的開發環境了。 當如果要恢復英文的介面,只要取消 Internationalization 的選項勾選,然後再次重新開啟 CodeBlocks 就回到英文的開發環境。 後記: CodeBlocks 翻譯文件網站 要下載 .mo or .po 檔案需要等待系統回復信件到 Email 信箱,無法及時處理,將這些檔案儲存在 Google Driver 的 src/CodeBlocks 目錄,以後可以從這裡直接取用。

cmd 程式無法執行的解決步驟

因為要設定 cmd 的編碼方式為 Unicode 編碼( chcp 65001),可能不小心修改了編碼,而導致cmd 無法開啟,主要的原因是:「cmd 變成沒有編碼」,所以才造成 cmd 無法開啟。在 Windows 8 中要恢復 cmd 編碼的步驟如下: 1. 滑鼠移到左上角,會出現功能的選項,點選「搜尋」的圖示 2. 在輸入的格子中,輸入「cmd」但是不要按下 enter 3. 滑鼠移到「cmd 命令提示字元」,,按下「滑鼠右鍵」 4. 下面會出現一些選項,點選「開啟檔案位置」,如此可以找到 cmd 命令提示字元的位置 5. 在「 命令提示字元」檔案中按下滑鼠右鍵,並點選「內容」 6. 點選「選項」,把「950 - Big 5 繁體中文」的編碼加入  

洗鏡光 - DCview.com達人部落格

要找 working set 的資料,從 [1] 的網站中得到他寫的作業系統筆記,而他筆記的內容大部分是從洗鏡光老師投影片的內容整理而來,於是 google "洗鏡光" 找的洗鏡光老師的投影片,結果是:「洗鏡光 - DCview.com達人部落格」,這是介紹「相機」的網站阿,怎麼是洗鏡光 老師的 blog 呢? 後來自己認為:「洗鏡光老師不可能沒有自己的網頁」,於是在「程式設計俱樂部」論壇[2]中找到洗鏡光老師的發言,其中有老師的英文名字(   shene ),再使用 shene 找,於是在找到洗鏡光老師[3]在美國的網站。從老師英文的網站中,在得知老師在台灣的網站就是「洗鏡光 - DCview.com達人部落格」,繞了一大圈才在「文章列表-- 電子計算機(電腦)科學 (3)」中,真正找到洗鏡光老師的投影片。 在 blog 中,另外有2篇文章,有一篇是說明「浮點數精確度」的問題,是值得詳細閱讀。 -------------------------------------------------------------- [1]  http://nixchun.pixnet.net/blog/category/523852 [2]  http://www.programmer-club.com.tw/ [3]  http://blog.dcview.com/blog.php?m=Bj8CZQ%3D%3D