Page tree
Skip to end of metadata
Go to start of metadata

基础介绍

Boost.Build 主要是为了在不同平台上使用不同的编译器轻松编译和安装Boost C++库,不过也可以用于任何普通的C/C++项目,当然它也只支持C++和C项目。

主要通过使用 b2 这个程序来使用进行操作,可以去github下载编译安装 https://github.com/boostorg/build

b2配置文件叫Jamfile.jam,有jam后缀的文件叫Jamfiles。 如果b2发现当前目录下有Jamfile, 它会逐层查找父目录中的Jamfiles。b2逐层查找上级目录,直到找到Jamroot.jam文件。Jamroot.jam与Jamfile.jam没有区别, 它只是告诉b2不需要再查找下去。

如果是一个小项目,并且只需要一个配置文件,那么可以只要Jamroot.jam, 而不要Jamfile.jam。

b2在启动的时候,首先需要加载编译系统(找到 boost-build.jam ),可以通过环境变量设置(BOOST_ROOT或BOOST_BUILD_PATH)。

指令规则

需要特别注意:冒号左右都需要空格,结尾的分号也需要空格。

exe

从源文件hello.cpp编译hello执行程序:

exe hello : hello.cpp ;

可以强制指定构建类型或默认构建类型:

# 第三个参数 <variant> 强制指定构建类型
exe hello : hello.cpp : <variant>release ; 

# 第四个参数 <variant> 指定默认构建类型
exe hello : hello.cpp : : <variant>release ; 
# 同时构建debug和release
exe hello : hello.cpp : : <variant>debug <variant>release ; 

还可以定义预处理指令<define>:

# define不是互斥的,所以WIN32和_WIN32同时对编译生效
exe hello : hello.cpp : <define>WIN32 <define>_WIN32 : <variant>debug <variant>release ; 

指定优化:

# 这样会构建出四个目标版本,因为variant和optimization都是互斥的
exe hello : hello.cpp : : <variant>debug <variant>release <optimization>speed <optimization>off ; 

lib

编译一个库:

lib world : world.cpp ; 

可以指定为静态链接库:

lib world : world.cpp : <link>static ; 

install 

安装编译目标:

exe hello : hello.cpp ;
install "C:/Program Files/hello" : hello ;

也可以换种编写方式,使用<location>(更推荐):

exe hello : hello.cpp ;
install install-bin : hello : <location>"C:/Program Files/hello" ;

还支持条件属性:

exe hello : hello.cpp ;
install install-bin : hello : <target-os>windows:<location>"C:/Program Files/hello" <target-os>linux:<location>/usr/local/bin ; 

glob

通配符,比如匹配多个源码文件:

exe hello : [ glob *.cpp ] ;

项目结构

build-project

大型项目中一般会包含多个jamfile,在项目的根目录通常有一个 Jamroot.jam ,在各个子目录中有 Jamfile.jam 文件。

当在根目录运行 b2 时,如果希望生成整个项目,包含子目录中的目标,则需要在 Jamfile 中显示引用:

build-project hello ;
build-project world ;

project

给当前目录和子目录中的 Jamfile 设置选项,参数的顺序没有要求:

# default-build 用来指定默认构建类型
project : default-build release ; 

# requirements 用来设置不能修改的选项
project : requirements <variant>release ; 

引用

假设 hello 依赖子目录的 world 库:

lib world : world.cpp ; 

那么 hello 的 Jamfile 可以这样子写:

# variant会进行传播,所以world也会同时生成debug和release两个版本
exe hello : hello.cpp world : : <variant>debug <variant>release ; 

# define不会进行传播,所以WIN32只作用与 hello.cpp
exe hello : hello.cpp world : <define>WIN32 : <variant>debug <variant>release ; 

use-project

定义另一个目录中Jamfile的别名:

project : requirements <variant>release ; 
use-project /boost : C:/boost_1_39_0 ; 
build-project hello ; 
build-project world ; 

这样子目录的Jamfiles就可以直接使用了:

exe hello : hello.cpp world /boost//filesystem ;

# 还可以指定静态链接
exe hello : hello.cpp world /boost//filesystem/<link>static ; 



参考:



  • No labels
Write a comment...