现在的位置: 首页 > 操作系统 > 正文

boost库编译,windows/linux

2018年10月05日 操作系统 ⁄ 共 3571字 ⁄ 字号 评论关闭

Windows(Linux在下面)
1.下载安装包boost_1_53_0

http://www.boost.org/users/download/

http://sourceforge.net/projects/boost/files/boost/1.53.0/
2.解压缩到d:/boost目录下

3.编译bjam

(1)从vs2010的工具菜单进入命令提示窗口(单击“开始”按钮,指向“所有程序”,指向“Microsoft Visua l Stuio 2010”,指向"工具",然后单击“Visual Studio 2010 command prompt(命令提示)”
(2)cd到d:/boost/boost_1_53_0下执行bootstrap.bat,然后,会在d:/boost/boost_1_53_0/下生成bjam.exe,
4.设定编译环境(加入红色字体)
修改user-config.jam (d:/boost/boost_1_53_0/tools/build/v2/user-config.jam) 的MSVC configuration
# MSVC configuration
# Configure msvc (default version, searched in standard location
# and PATH).
# using msvc ;
using msvc : 10.0 : : <compileflags>/wd4819 <compileflags>/D_CRT_SECURE_NO_DEPRECATE <compileflags>/D_SCL_SECURE_NO_DEPRECATE <compileflags>/D_SECURE_SCL=0 ;
5.將目录cd到d:/boost/boost_1_53_0/下执行?
(1) 编译boost库
    bjam --without-python --toolset=msvc-10.0 --build-type=complete --prefix="d:/boost/boost_1_53_0" stage (这一步需要等待很长时间,大概一个小时左右……)
运行完后(弹出输入提示符)
(2) 则安装,输入:
    bjam --with-python --toolset=msvc-10.0 --build-type=complete --prefix="d:/boost/boost_1_53_0" install  (这一步时间也较长)。
6.设定vs2010环境。
Tools -> Options -> Projects and Solutions -> VC++ Directories?
在Library files加上D:\boost\boost_1_53_0\stage\lib
在Include files加上D:\boost\boost_1_53_0\include\boost-1_53
7.测试实例:
#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) << " " );
}


Linux平台Boost的编译方法


Boost的编译使用的不是已经成为公认标准的make,而是专门为Boost开发的工具bjam(boost jam)。
本文以boost1.40为例,在Linux平台的编程环境为gcc4.4.1,假设Boost代码在/usr/src/boost_1_40_0目录,步骤如下。
1. 获得bjam
获得bjam有3种方式,如下:
方法1:Boost网站上提供了各种平台上预编译好的bjam可执行程序,可直接下载使用。
方法2:从Boost源代码中提供的bjam代码编译出可执行程序。
方法3:通过bootstrap程序生成bjam。
方法1:
# apt-get install bjam
方法2:
(1) # cd /usr/src/boost_1_40_0/tools/jam/src
(2) # ./build.sh
编译完成后:
生成的可执行文件为
# /usr/src/boost_1_40_0/tools/jam/src/bin.linuxx86/bjam
需要将bjam.exe拷贝到源代码所在的目录
? # cp bin.linuxx86/bjam /usr/src/boost_1_40_0
方法3:
(1) # cd /usr/src/boost_1_40_0
(2) # ./bootstrap.sh

该方法直接在Boost源代码目录下生成bjam文件。
2. 修改bjam配置
使用bjam前,需要修改bjam的配置文件。Linux平台的配置文件:
/usr/src/boost_1_40_0/tools/build/v2/user-config.jam
修改如下。注:笔者在实验时,不修改貌似也很正常。
将43行的“# using gcc ;”前的注释符号‘#’去掉,表明用到使用gcc编译。
如果要使用STLport作为其标准库,将75行前的‘#’去掉。
3. 完整编译Boost
对Boost进行完整编译,生成所有调试版、发行版的静态库和动态库。
方法1:
# cd /usr/src
# wget  http://sourceforge.net/projects/boost/files/boost/1.40.0/boost_1_40_0.tar.bz2
# tar --bzip2 -xvf boost_1_40_0.tar.bz2
# cd boost_1_40_0
# bjam --toolset=gcc --build-type=complete stage

# ./bjam --build-type=complete --layout=versioned ? ? ? ? ? ?//库名带gcc版本

# ./bjam --build-type=complete --layout=tagged stage

# ./bjam
方法2:
# cd /usr/src/boost_1_40_0/tools/jam
# ./build_dist.sh
也会完成Linux平台bjam和Boost的所有编译工作,并生成所有调试版、发行版的静态库和动态库。但不推荐该方式。
编译成功后,将在/usr/src/boost_1_40_0/bin.v2目录下生成诸多文件,包括.a和.so.1.40.0文件,这就是在Linux平台要使用Boost需要的,其他的文件可以删除。
安装
# ./bjam install
将生成的库默认安装到/urs/local/lib目录。
也可以把编译和安装写在一起,如:./bjam --build-type=complete --layout=tagged stage install
4 部分编译Boost
完整编译Boost费时费力,且这些库在开发过程中并不一定全部用到,因此,bjam也支持用户自行选择要编译的库。
在完全编译的基础上,使用--with或者—without选择可以打开或者关闭某个库的编译,例如,仅仅编译date_time库:
# cd /usr/src/boost_1_40_0
# ./bjam --toolset=msvc --with-date_time --build-type=complete stage
当然,bjam还有很多其他选项,如指定安装路径,指定debug或release等,可参考bjam文档或帮助。
NOTE:1.48版本已经支持b2进行编译,功能和bjam类似。
5. bjam的一些参数:
--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 显示需要编译的库名称

抱歉!评论已关闭.