WSL2 的 Ubuntu 编译和安装 boost

说明

这个系列将记录自己科研搬砖过程中的点点滴滴。由于有些代码比较目前还属于特殊情况,不会在记录中明确特别的细节。

系统环境

开始正式的科研搬砖。今天在新机器上安装 boost,环境为 Win10 下的 WSL2,Ubuntu 版本 18.04,boost 版本为 1.74.0。

编译安装 boost

更新系统

老样子,先将系统更新到最新,免得出错。

$ sudo apt update
$ sudo apt upgrade

安装依赖库

boost 的依赖库主要有四个:MPI 库(mpi-default-dev)、正则表达式的UNICODE字符集(libicu-dev)和 bzlib(libbz2)。

编译和安装 boost

获取 boost 源码

我使用 win10 下载了 boost 1.74.0 的源码,https://www.boost.org/users/history/version_1_74_0.html。放在 c:/xxx/boost_1_74_0.tar.bz2 中。

在 WSL2 中,使用命令行直接拷贝过来即可。

$ cp /mnt/c/xxx/boost_1_74_0.tar.bz2 .

解压

tar -jxvf boost_1_74_0.tar.bz2

编译

使用 bootstrap.sh。

$ cd boost_1_74_0/
$ ./bootstrap.sh
Building Boost.Build engine with toolset gcc... tools/build/src/engine/b2
Detecting Python version... 2.7
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... /usr
Generating Boost.Build configuration in project-config.jam for gcc...

Bootstrapping is done. To build, run:

    ./b2

To generate header files, run:

    ./b2 headers

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/build/

使用 b2。

$ sudo ./b2
Performing configuration checks

    - default address-model    : 64-bit
    - default architecture     : x86
Building the Boost C++ Libraries.


    - C++11 mutex              : yes
    - lockfree boost::atomic_flag : yes
    - has stat::st_mtim        : yes
    - has stat::st_mtimensec   : no
    - has stat::st_mtimespec   : no
    - Boost.Config Feature Check: cxx11_auto_declarations : yes
    - Boost.Config Feature Check: cxx11_constexpr : yes
    - Boost.Config Feature Check: cxx11_defaulted_functions : yes
    - Boost.Config Feature Check: cxx11_final : yes
    - Boost.Config Feature Check: cxx11_hdr_mutex : yes
    - Boost.Config Feature Check: cxx11_hdr_tuple : yes
    - Boost.Config Feature Check: cxx11_lambdas : yes
    - Boost.Config Feature Check: cxx11_noexcept : yes
    - Boost.Config Feature Check: cxx11_nullptr : yes
    - Boost.Config Feature Check: cxx11_rvalue_references : yes
    - Boost.Config Feature Check: cxx11_template_aliases : yes
    - Boost.Config Feature Check: cxx11_thread_local : yes
    - Boost.Config Feature Check: cxx11_variadic_templates : yes
    - has_icu builds           : yes
warning: Graph library does not contain MPI-based parallel components.
note: to enable them, add "using mpi ;" to your user-config.jam.
note: to suppress this message, pass "--without-graph_parallel" to bjam.
    - zlib                     : yes
    - bzip2                    : yes
    - lzma                     : no
    - zstd                     : no
    - lzma                     : no  (cached)
    - has_lzma_cputhreads builds : no
    - iconv (libc)             : yes
    - icu                      : yes
    - native-atomic-int32-supported : yes
    - native-syslog-supported  : yes
    - pthread-supports-robust-mutexes : yes
    - compiler-supports-ssse3  : yes
    - compiler-supports-avx2   : yes
    - gcc visibility           : yes
    - long double support      : yes
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.
    - Boost.Config Feature Check: cxx11_static_assert : yes
    - std::fstream is moveable and swappable : yes
    - Has Large File Support   : yes
    - libbacktrace builds      : yes
    - addr2line builds         : yes
    - WinDbg builds            : no
    - WinDbgCached builds      : no
    - BOOST_COMP_GNUC >= 4.3.0 : yes
    - zlib                     : yes
    - bzip2                    : yes
    - lzma                     : no
    - zstd                     : no

Component configuration:

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

...patience...
...patience...
...patience...
...patience...
...patience...
...found 16378 targets...
...updating 1792 targets...

耐心等完成,再使用 install。

$ sudo ./b2 install
[sudo] password for zhouyi:
Performing configuration checks

    - default address-model    : 64-bit (cached)
    - default architecture     : x86 (cached)
    - C++11 mutex              : yes (cached)
    - lockfree boost::atomic_flag : yes (cached)
    - has stat::st_mtim        : yes (cached)

这样 boost 就会安装的缺省路径 /user/local,也就是头文件在 /usr/local/include/,库文件(lib)在 /usr/local/lib。

这样 boost 库就安装并且编译成功了。

扫描二维码关注公众号,回复: 11868517 查看本文章

测试

测试程序

我们就使用 boost 自带的测试程序。

#include <iostream>
#include <boost/bind.hpp>
#include <boost/version.hpp>

using namespace std;
using namespace boost;

int fun(int x,int y){
    return x+y;
}

int main(){
    cout << "boost version" << BOOST_VERSION <<endl;
    cout << "boost lib version" << BOOST_LIB_VERSION <<endl;
    
    int m=1;
    int n=2;
    cout<<boost::bind(fun,_1,_2)(m,n)<<endl;
    return 0;
}
$ g++ boots.cpp -g -o boost
In file included from boots.cpp:2:0:
/usr/local/include/boost/bind.hpp:41:1: note: #pragma message: The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior.
 )
 ^
$ ./boost
boost version107400
boost lib version1_74
3

这样,我们就完成了 Ubuntu 18.04 下 boost 1.74.0 的编译和安装。

可以开始科学搬砖了。

猜你喜欢

转载自blog.csdn.net/justidle/article/details/108758076