在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,敲入如下代码:

[cpp]  view plain copy
  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>  
[cpp]  view plain copy
  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 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值