boost库的安装



boost库的安装和使用略记


一、对于Windows平台,在VS2013中安装配置boost_1_58_0库(默认已安装vs2013)

1.www.boost.org下载相应的代码包,我下载的是boost_1_58_0.tar.bz2

2. 解压boost文件到本地目录(如G:\boost_1_58_0),可以发现解压后的文件中有一个bootstrap.bat文件。

然后以管理员身份打开cmd窗口,转到bootstrap.bat文件所在路径,执行bootstrap.bat文件;

3.上述命令执行完毕后,可以发现G:\boost_1_55_0下新生成了一个bjam.exe文件

4.在命令窗口中输入语句:bjam.exe,执行bjam.exe

此过程将默认根据系统已经安装好的编译工具(VS2008,2010,2012,2013)等编译相应的Lib文件、头文件等。(此步骤大概需要10分钟)

5.添加boostest工程的包含目录(include头文件)和库目录(lib库文件)

工程名->配置属性->c/c++->常规->附加包含目录,添加:  G:\boost_1_58_0

工程名->配置属性->链接器->常规->附加库目录,添加:    G:\boost_1_58_0\stage\lib

在工程中新建文件,内容为


  1. #include <boost/lexical_cast.hpp>       
  2. #include <iostream>       
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     using boost::lexical_cast;  
  8.     int a = lexical_cast<int>("123");  
  9.     double b = lexical_cast<double>("123.0123456789");  
  10.     string s0 = lexical_cast<string>(a);  
  11.     string s1 = lexical_cast<string>(b);  
  12.     cout << "number: " << a << "  " << b << endl;  
  13.     cout << "string: " << s0 << "  " << s1 << endl;  
  14.     int c = 0;  
  15.     try  
  16.     {  
  17.         c = lexical_cast<int>("abcd");  
  18.     }  
  19.     catch (boost::bad_lexical_cast& e)  
  20.     {  
  21.         cout << e.what() << endl;  
  22.     }  
  23.     return 0;  
  24. }  
#include <boost/lexical_cast.hpp>     
#include <iostream>     
using namespace std;

int main()
{
	using boost::lexical_cast;
	int a = lexical_cast<int>("123");
	double b = lexical_cast<double>("123.0123456789");
	string s0 = lexical_cast<string>(a);
	string s1 = lexical_cast<string>(b);
	cout << "number: " << a << "  " << b << endl;
	cout << "string: " << s0 << "  " << s1 << endl;
	int c = 0;
	try
	{
		c = lexical_cast<int>("abcd");
	}
	catch (boost::bad_lexical_cast& e)
	{
		cout << e.what() << endl;
	}
	return 0;
}
如果该文件可以编译和执行,则说明在VS2013中成功安装和配置boost_1_58_0库,否则安装和配置失败。

图片说明可以参考 http://jingyan.baidu.com/album/11c17a2c765763f446e39dc1.html?picindex=1


二、在Linux平台,在gcc和g++环境下安装boost库

1)到www.boost.org下载相应的代码包,我下载的是boost_1_58_0.tar.bz2

(2)进入自己的工作目录执行解压操作:

  root@test# bzip2 -d boost_1_58_0.tar.bz2

  root@test# tar -xvf  boost_1_58_0.tar

  root@test#$ cd boost_1_58_0

 root@boost_1_58_0# ./bootstrap.sh //生成安装工具bjamb2

(3)这里利用b2工具进行安装,可以使用./b2 --help 查看命令选项

(4)准备安装boost

 root@boost_1_58_0#  sudo ./b2 install

安装完毕后的头文件默认是在/usr/local/include目录下,.a.so/usr/local/lib目录下。

然后,将需要使用的库sudo cp/usr/lib 。同时,向ld.so.conf文件中添加libboost_system.so.1.58.0所在的目录路径

否则,在执行代码时,ldd会提示找不到.so文件。

./main: error while loading shared libraries: libboost_serialization.so.1.58.0: cannot open shared object file: No such file or directory

方法为:

 root@boost_1_58_0#  sudo vi /etc/ld.so.conf

#添加向ld.so.conf文件中添加libboost_system.so.1.58.0所在的目录路径

root@boost_1_58_0#  sudo ldconfig

(5)编译和运行一个使用boost库的C++示例文件,检验boost库是否安装和配置成功。

表1. bjam(或b2)的其它参数
--build-dir=<builddir> 编译的临时文件会放在builddir里(编译完就可以把它删除了)
--stagedir=<stagedir> 存放编译后库文件的路径,默认是stage
--build-type=complete 编译所有版本,不然只会编译一小部分版本(相当于:
variant=release,threading=multi;
link=shared|static;runtime-link=shared)
variant=debug|release 决定编译什么版本(Debug or Release)
link=static|shared 决定使用静态库还是动态库
threading=single|multi 决定使用单线程还是多线程库
runtime-link=static|shared 决定是静态还是动态链接C/C++标准库
--with-<library> 只编译指定的库,如输入--with-regex就只编译regex库了
--show-libraries 显示需要编译的库名称

猜你喜欢

转载自blog.csdn.net/qq_36038987/article/details/79696798