在CodeBlocks環境下使用boost庫

轉自:http://blog.csdn.net/hejianhua/article/details/12910409

codeblocks中怎麼用第三方庫,比如ACE,ICE,POCO,QT等
我在poco官網上下載了其支持windows平臺的源代碼,編譯完成後,它就自動和vs掛鉤了,在vs系列的IDE上都很容易用。但是現在我想用codeblocks(windows下用),我在工程中bin,lib,.h的的路徑都添加好了,可是編譯不能通過,而使用同樣的方法在vs中就完全沒有問題,爲什麼呢?鬱悶!

我在windows下都用不起,更別說linux了,所以請大家先幫我解決下這個問題吧。這個問題,同問ace,ice,它們都能在vs下搞,但是放在codeblocks上就要現怪相!我有點白,大家說詳細點,感謝~

------解決方案--------------------------------------------------------
第一步:編譯第三方庫,得到頭文件和庫,例如路徑關係:
D:\MyLib\include
D:\MyLib\lib
在include中放頭文件,在lib中放置庫文件。

第二步:創建全局變量,菜單:Settings > Global variables, New一個新的,選名字,例如MYLIB
Base: /usr/local/ACE/ACE_wrappers
include: $(BASE)\ace
lib: $(BASE)\lib

第三步:設置工程搜索路徑,菜單:Project > build options > Search directories
Compiler: $(#MYLIB)  // 解釋:因爲文件中包含的是 #include "ace/tas.h",所以搜索路徑不需要Compiler: $(#MYLIB.ace)
Linker: $(#MYLIB.lib)
在Link settings -> Link libraries 中添加動態庫 $(#MYLIB)/lib/libACE.so.6.2.2

即可使用。
如果不想添加全局變量,則第三步時,使用絕對路徑,來分別設置頭文件和庫文件的查找路徑。

------解決方案--------------------------------------------------------
探討

引用:
第一步:編譯第三方庫,得到頭文件和庫,例如路徑關係:
D:\MyLib\include
D:\MyLib\lib
在include中放頭文件,在lib中放置庫文件。

第二步:創建全局變量,菜單:Settings > Global variables, New一個新的,選名字,例如MYLIB
Base: D:\MyLib
include: $……

 

-------------------------------------------------------------------------------------------------------------------------------

作者:朱金燦

來源: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,敲入如下代碼:

  1. <SPAN style="FONT-SIZE: 18px">   #include <stdlib.h>  
  2.     #include <iostream>   
  3.     using std::cout;  
  4.     using std::wcout;  
  5.     using std::endl;  
  6.     #include <string>   
  7.     using std::string;  
  8.     using std::wstring;  
  9.   
  10.     #include <boost/algorithm/string.hpp>   
  11.     #include <boost/filesystem/path.hpp>  
  12.     #include "boost/filesystem/operations.hpp"   
  13.     #include <boost/format.hpp>   
  14.   
  15. int main()  
  16. {  
  17.     // ANSI字符的格式化   
  18.         cout << boost::format( "%1% %2%" ) % "Hell" % "Low" <<endl;  
  19.         string s1 = boost::str( boost::format( "%2% %1%" ) % "Hell" % "Low" );  
  20.         cout << s1 << endl;  
  21.         // UNICODE字符的格式化   
  22.         wcout << boost::wformat( L"%s %X" ) % L"-1 is" % -1 << endl;  
  23.         wstring s2 = boost::str( boost::wformat( L"%2$s %1$.2f" ) % 3.141592 % L"Version" );  
  24.         wcout << s2 << endl;  
  25.         // 獲取應用程序所在目錄(ANSI字符),注意是boost::filesystem::path  
  26.         string AnsiPath = boost::filesystem::initial_path<boost::filesystem::path>().string();  
  27.         cout<<AnsiPath<<endl;  
  28.         // 獲取應用程序所在目錄(UNICODE字符),注意是boost::filesystem::wpath  
  29.         wstring UnicodePath = boost::filesystem::initial_path<boost::filesystem::wpath>().string();  
  30.         wcout<<UnicodePath<<endl;  
  31.         system("PAUSE");  
  32.         return 0;  
  33. }  
  34. </SPAN>  
  1. <span style="font-size:18px">   #include <stdlib.h>  
  2.     #include <iostream>  
  3.     using std::cout;  
  4.     using std::wcout;  
  5.     using std::endl;  
  6.     #include <string>  
  7.     using std::string;  
  8.     using std::wstring;  
  9.   
  10.     #include <boost/algorithm/string.hpp>  
  11.     #include <boost/filesystem/path.hpp>  
  12.     #include "boost/filesystem/operations.hpp"  
  13.     #include <boost/format.hpp>  
  14.   
  15. int main()  
  16. {  
  17.     // ANSI字符的格式化  
  18.         cout << boost::format( "%1% %2%" ) % "Hell" % "Low" <<endl;  
  19.         string s1 = boost::str( boost::format( "%2% %1%" ) % "Hell" % "Low" );  
  20.         cout << s1 << endl;  
  21.         // UNICODE字符的格式化  
  22.         wcout << boost::wformat( L"%s %X" ) % L"-1 is" % -1 << endl;  
  23.         wstring s2 = boost::str( boost::wformat( L"%2$s %1$.2f" ) % 3.141592 % L"Version" );  
  24.         wcout << s2 << endl;  
  25.         // 獲取應用程序所在目錄(ANSI字符),注意是boost::filesystem::path  
  26.         string AnsiPath = boost::filesystem::initial_path<boost::filesystem::path>().string();  
  27.         cout<<AnsiPath<<endl;  
  28.         // 獲取應用程序所在目錄(UNICODE字符),注意是boost::filesystem::wpath  
  29.         wstring UnicodePath = boost::filesystem::initial_path<boost::filesystem::wpath>().string();  
  30.         wcout<<UnicodePath<<endl;  
  31.         system("PAUSE");  
  32.         return 0;  
  33. }  
  34. </span>  

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 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章