在UBuntu 14.01中配置Boost库(及CodeBlocks)

来源:互联网 发布:天猫和淘宝有什么区别 编辑:程序博客网 时间:2024/06/05 08:01
在UBuntu 14.01中配置Boost库
前言:以下配置的步骤参考Boost官方文档以及其他一些CSDN上的博文。配置的编译环境是CodeBlocks v13,有关命令行的编译方法CSDN上介绍了很多,此处不在赘述。

步骤1:
首先下载Boost库。
Linux下推荐下面的链接。
http://www.boost.org/users/history/version_1_56_0.html


步骤2:配置Boost库
正如Boost库文档中说明的,Boost的大部分库以hpp文件提供,即类的声明和实现都放在同一文件中并且以源码提供,在使用时不需要做特别处理。因此,下载压缩包之后,将压缩包解压到合适路径下就可以使用它们了。在UBuntu中,推荐放置在/home或者/usr/local中的一处。例如,Boost的版本是1.56.0,那么可以将库展开到/usr/local/boost_1_56_0这个目录中。
之后,可以启动CodeBlocks,配置全局变量。具体操作为
菜单Settings->Global Variable,配置信息如下。

Global Variable Config - Boost


然后新建控制台工程(如下图)。



接下来,在新建的工程中,选择“Source”资源项新建main.cpp,将Boost的测试代码(取自Boost官网的Getting Started)粘贴一下。下图示例中给出了示例代码和编译后的输出结果。

图片
注意:编译之前要选择工程的“Build Options”然后配置一下Boost库的查找路径。
之后,按Ctrl+F9执行编译,就可以知道是否配置完毕。
 

Building Option Console Project



确认执行结果


从源码可以知道例子是测试Lamda表达式,功能是计算从标准控制台(即cin输入流)中输入的3个数字,每个数字各乘以3。因此,运行示例程序,结果如下图所示。

Test result of Sample code of Boost Lib


步骤3:编译二进制Boost库

由于Boost的配置在持续改进,使用Boost官网起步教程中的快速安装步骤,只需要3不就可以编译二进制库。


1)准备编译工具 b2;请注意,Boost库从1.50版开始,使用新的b2编译程序而不是bjam,其编译速度已大幅提升。

 在开始前,可以用./bootstrap.sh --help命令查看一下帮助,主要的参数执行如下(注意$是命令行提示符,不用输入)

$ cd path/to/boost_1_56_0

$ ./bootstrap.sh --help


此处,先按缺省配置操作。直接指定安装路径即可,注意下面红色的参数配置,其中YOUR_BOOST_LIB_DIR是你希望的Boost二进制库安装位置,譬如,你可以使用“binlib”作为目录名称,我的例子中是按照“stage”目录设置的。我的硬盘空间很充足,也
为了省事 ,是按照编译全部库进行操作(可以使用--with-libraries=lib1,lib2,...,的格式制定部分库进行编译。有关如何指定编译部分库文件的操作在CSDN上已有博文,此处不再赘述)。

以下为执行./bootstrap.sh的输出结果:

$ sudo sh ./bootstrap.sh --prefix=YOUR_BOOST_LIB_DIR

 

Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2

Detecting Python version... 2.7

Detecting Python root... /usr

Unicode/ICU support for Boost.Regex?... not found.

Generating Boost.Build configuration in project-config.jam...

Bootstrapping is done. To build, run:

    ./b2

   

To adjust configuration, edit 'project-config.jam'.

Further information:

   - Command line help:

     ./b2 --help

    

   - Getting started guide:

     http://www.boost.org/more/getting_started/unix-variants.html

    

   - Boost.Build  documentation:

     http://www.boost.org/boost-build2/doc/html/index.html

 


由于部分库编译时需要系统中有其他库文件支持,请执行以下操作,确认所有的依赖文件包都已安装。以下安装包中python2.6-dev和libbz2-dev特别重要,缺少这些库文件的支持,编译过程中python库和bzip库会无法编译。

$ sudo apt-get install python2.6-dev

$ sudo apt-get install libicu-dev

$ sudo apt-get install libbz2-dev


 

此步骤完成之后,就可以启动b2程序进行编译。为了确认编译的过程没有出错,建议将编译结果的输出重定向(可以使用">> build.log")到日志文件。因此,执行以下命令。(实际上,由于bjam已经过时,你即便启动bjam,也是调用b2程序。)


$ ./b2 install >build.log


如果输出到屏幕,控制台中的信息会由于内容过多,后输出的结果覆盖掉之前的输出结果。以下是编译开始时的信息摘录。
 

Building the Boost C++ Libraries.

Performing configuration checks

    - 32-bit                   : no  (cached)
    - 64-bit                   : yes (cached)
    - arm                      : no  (cached)
    - mips1                    : no  (cached)
    - power                    : no  (cached)
    - sparc                    : no  (cached)
    - x86                      : yes (cached)
    - lockfree boost::atomic_flag : yes (cached)
    - has_icu builds           : no  (cached)
warning: Graph library does not contain MPI-based parallel components.
note: to enable them, add "using mpi ;" to your user-config.jam
    - zlib                     : yes (cached)
    - iconv (libc)             : yes (cached)
    - icu                      : no  (cached)
    - icu (lib64)              : no  (cached)
    - compiler-supports-ssse3  : yes (cached)
    - compiler-supports-avx2   : yes (cached)
    - gcc visibility           : yes (cached)
    - long double support      : yes (cached)
warning: skipping optional Message Passing Interface (MPI) library.
note: to enable MPI support, add "using mpi ;" to user-config.jam.
note: to suppress this message, pass "--without-mpi" to bjam.
note: otherwise, you can safely ignore this message.
    - zlib                     : yes (cached)

Component configuration:

    - atomic                   : building
    - chrono                   : building
    - container                : building
    - context                  : building
    - coroutine                : building
    - date_time                : building
    - exception                : building
    - filesystem               : building
    - graph                    : building
    - graph_parallel           : building
    - iostreams                : building
    - locale                   : building
    - log                      : building
    - math                     : building
    - mpi                      : building
    - program_options          : building
    - python                   : building
    - random                   : building
    - regex                    : building
    - serialization            : building
    - signals                  : building
    - system                   : building
    - test                     : building
    - thread                   : building
    - timer                    : building
    - wave                     : building

...patience...
...patience...
...patience...
...patience...
...patience...
...found 11605 targets...
...updating 70 targets...


在所有库(如前述,根据你的配置需要库的多少有变化,缺省是编译所有需要编译的库)都成功编译后,最后几行会输出如下结果。
 

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

    /usr/local/boost_1_56_0

The following directory should be added to linker library paths:

    /usr/local/boost_1_56_0/stage/lib


 看到这些提示,恭喜你大功告成! 注意编译程序给出的路径提示,即你在使用时如何配置的路径,分别对应头文件路径和二进制库文件的路径。

步骤4:确认编译结果

确认编译结果同样可以使用官方网站上提供的例子程序,


Test sample for Boost binary lib


此时配置搜索路径(仍然是选择Build options在对话框中选择Search directories)就需要增加一个选项,如下图。


Console Project build option with binary lib

同时,还要配置链接库,如下图。

Linik config for Boost with binary lib

此时,编译例子程序遍可以成功完成。

在编译的可执行程序目录下新建一个文本文件sample.txt,粘贴以下测试信息并保存退出。

To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.
 
然后执行例子程序测试,结果如下图。

Reult for sampel code of testing Boost binary lib

可以看到例子给的是使用正则表达式的程序,功能是在输入流中检索Subject字符串之后的内容(即主题的内容),因此输出结果是符合期望的。


最后要说的几句话:
1)如果亲的硬盘空间有限,在所有任务结束后,可以切换到刚才编译时执行的目录下,使用以下命令清理编译中间结果。中间结果存放在“bin.v2”目录中。注意:如果清理了编译中间结果,以后Boost库更新的时候,所有编译都必须从头来过。
 
$ ./b2 --clean
 
2)整个安装过程在不费周章的情况下,大约需要1小时。编译过程所耗费的时间大致是半小时,我用的硬件配置就是Lenovo Y470,i3+6GB 内存,不算高。

3)本文中的所有步骤我都已经实际测试过,如果试验过程遇到其他问题可以留言。最后请大家尊重我的劳动成果,转载请注明出处。多谢! 


0 0
原创粉丝点击