在CodeBlocks環境下使用boost庫

來源:互聯網
上載者:User

作者:朱金燦

來源:http://blog.csdn.net/clever101

 

     首先請先編譯或安裝boost庫,使用CodeBlocks編譯boost庫具體見:Boost庫在CodeBlocks環境下的編譯。

以下內容主要翻譯自:BoostWindows Quick Ref ,我所用的編譯環境為Win 7家庭版,CodeBlocks V10.05, mingw32 v4.4.1,boostv1.42

 

1. 為boost庫建立一個CodeBlocks全域變數

1)開啟“Settings”菜單——>"Global variables..."功能表項目,彈出如下對話方塊,單擊的對話方塊上的New按鈕,

2) 在彈出的對話方塊上輸入你要建立的全部變數名,例如boost,如:

3)設定全域變數的base目錄、lib目錄和include目錄,具體如:

Base目錄為你的boost安裝目錄,也是你在編譯boost庫的—prefix選項的參數值,如:

你的bjam命令為:

bjam install--toolset=gcc--prefix="C:\zjc\PluginFramework\boost_1_42_0"--build-type=complete 

那麼base目錄為C:\zjc\PluginFramework\boost_1_42_0

Include和lib目錄很容易理解,就是boost庫的標頭檔和庫檔案所在的目錄。

 

現在我們使用一下這個全域變數,

1.     CodeBlocks建立一個控制台工程TestConsole,敲入如下代碼:

   #include <stdlib.h>    #include <iostream>    using std::cout;    using std::wcout;    using std::endl;    #include <string>    using std::string;    using std::wstring;    #include <boost/algorithm/string.hpp>    #include <boost/filesystem/path.hpp>    #include "boost/filesystem/operations.hpp"    #include <boost/format.hpp>int main(){    // ANSI字元的格式化        cout << boost::format( "%1% %2%" ) % "Hell" % "Low" <<endl;        string s1 = boost::str( boost::format( "%2% %1%" ) % "Hell" % "Low" );        cout << s1 << endl;        // UNICODE字元的格式化        wcout << boost::wformat( L"%s %X" ) % L"-1 is" % -1 << endl;        wstring s2 = boost::str( boost::wformat( L"%2$s %1$.2f" ) % 3.141592 % L"Version" );        wcout << s2 << endl;        // 擷取應用程式所在目錄(ANSI字元),注意是boost::filesystem::path        string AnsiPath = boost::filesystem::initial_path<boost::filesystem::path>().string();        cout<<AnsiPath<<endl;        // 擷取應用程式所在目錄(UNICODE字元),注意是boost::filesystem::wpath        wstring UnicodePath = boost::filesystem::initial_path<boost::filesystem::wpath>().string();        wcout<<UnicodePath<<endl;        system("PAUSE");        return 0;}


2. 在工程樹節點上按右鍵,在彈出的右鍵菜單上單擊Build Option…功能表項目,如:

3.在彈出的對話方塊中選擇Search directories選項卡,然後單擊對話方塊中的add按鈕,在彈出的對話方塊中輸入:$(#boost),具體如:


    看到這,你可能覺得比較熟悉,這個CodeBlocks全域變數不相當於一個作業系統環境變數嗎?是的,它起的正是這個作用。

 

4.選擇“Linker setting”選項卡,填入你的工程要連結到到的具體的boost庫,具體如:

 


    一個小小的問題,在使用boost庫時只需在定義了BOOST_ALL_DYN_LINK宏,同時指定了庫檔案所在的檔案夾就能實現自動連結而不必指定要連結到的具體庫,這個我在VS環境下已經多次使用過,但我在CodeBlocks下定義了BOOST_ALL_DYN_LINK,如:



  但是還是得指定連結到的具體庫,這是為什麼呢?知道的大俠請具體指點下。

 

參考文獻:

1. BoostWindows Quick Ref








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.