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 显示需要编译的库名称

另外可以参考:

Linux中安装boost库详解:http://blog.csdn.net/zhoudaxia/article/details/4292953

http://www.th7.cn/Program/cp/201303/127637.shtml

含泪播种的人一定能含笑收获。

Linux平台Boost的编译方法

相关文章:

你感兴趣的文章:

标签云: