• MacOS 中Boost的安装和使用


    Boost是一个功能强大、构造精巧、跨平台、开源并且完全免费的C++程序库,有着“C++‘准’标准库”的美誉,值得每位C++程序员学习使用。

    1 安装Boost
    1.1 使用源码安装

    下载Boost源码
    解压放在任意目录,例如/usr/local/boost_1_63_0
    ./bootstrap.sh
    ./b2 headers
    ./b2
    留意运行日志头文件目录 /usr/local/boost_1_63_0, lib目录/usr/local/boost_1_63_0/stage/lib
    打开源码中index.html查看使用文档

    1.2 使用Homebrew安装

    下载安装HomeBrew
    brew install boost
    留意运行日志会显示头文件目录 /usr/local/Cellar/boost/1.60.0_2/include, lib目录/usr/local/Cellar/boost/1.60.0_2/lib

    1.3 使用MacPort安装

    下载安装MacPort
    sudo port install boost

    2 在XCode项目中使用Boost

    新建一个Command Line Tool项目
    在Build Setings - Header Search Paths 增加头文件目录
    替换main.cpp中代码,运行!输入任意数字回车可看到结果。

    #include 
    #include 
    int main(int argc, const char * argv[]) {
        printf("Please input any number:");
        using namespace boost::lambda;
        typedef std::istream_iterator in;
        std::for_each(in(std::cin), in(), std::cout << (_1 * 3) << " " );
        return 0;
    }
    
    3 在XCode项目中使用Boost Lib库

    Boost的很多功能都是直接在hpp头文件里实现的,比如上面的lambda例子不用导入任何lib就可以运行了。但也有一部分需要依赖指定lib库才能使用。比如下面这个正则表达式的例子:

    #include 
    #include 
     
    int main(int argc, const char * argv[]) {
        std::string   str = "2013-08-15";
        boost::regex  rex("(?[0-9]{4}).*(?[0-9]{2}).*(?[0-9]{2})");
        boost::smatch res;
        
        std::string::const_iterator begin = str.begin();
        std::string::const_iterator end   = str.end();
        
        if (boost::regex_search(begin, end, res, rex))
        {
            std::cout << "Day:   " << res ["day"] << std::endl
            << "Month: " << res ["month"] << std::endl
            << "Year:  " << res ["year"] << std::endl;
        }
    }
    
    
    3.1 使用静态库

    Build Setings - Other linker flags /usr/local/boost_1_63_0/stage/lib/libboost_regex.a

    使用命令行编译相当于

    c++ -I /usr/local/boost_1_63_0 main.cpp -o main /usr/local/boost_1_63_0/stage/lib/libboost_regex.a
    ./main
    

    如果这里直接使用lboost_regex, Xcode默认加载动态库。实际运用中可以考虑将目录中的动态库删除,只保留静态库,并在Build Setings - Library Search Paths 增加lib文件目录。

    3.2 使用动态库
    1. 在Build Setings - Library Search Paths 增加lib文件目录
    2. 将lib文件目录中的libboost_regex.dylib文件拖入项目
    3. 确保在Build Phases - Link Bindary With Libraries中已经有该库
    4. 在Build Phases - Copy Files, 复制libboost_regex.dylib到Products Directory
      使用命令行编译相当于
    c++ -I /usr/local/boost_1_63_0 main.cpp -o main -L/usr/local/boost_1_63_0/stage/lib/ -lboost_regex
    cp /usr/local/boost_1_63_0/stage/lib/libboost_regex.dylib ./
    ./main
    

    最终安装目录:

    /usr/local/Cellar/boost/1.67.0_1
    

    设置头文件为/usr/local/Cellar/boost/1.67.0_1/include/,库文件为/usr/local/Cellar/boost/1.67.0_1/lib/
    使用brew安装结果:

    brew install boost
    Updating Homebrew...
    ==> Downloading https://homebrew.bintray.com/bottles-portable-ruby/portable-ruby-2.3.7.leopard_64.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring portable-ruby-2.3.7.leopard_64.bottle.tar.gz
    ==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
      https://github.com/Homebrew/brew#donations
    ==> Migrating tap caskroom/cask to homebrew/cask...
    Changing remote from https://github.com/Caskroom/homebrew-cask to https://github.com/Homebrew/homebrew-cask...
    Moving /usr/local/Homebrew/Library/Taps/caskroom/homebrew-cask to /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...
    ==> Auto-updated Homebrew!
    Updated 4 taps (homebrew/science, homebrew/core, homebrew/services, homebrew/cask).
    ==> New Formulae
    amtk                                   gptfdisk                               patchelf
    gmt@4                                  nuxeo                                  zstd
    gnatsd                                 nvc
    ==> Renamed Formulae
    cdiff -> ydiff                         php56 -> php@5.6                       rebar@3 -> rebar3
    crystal-lang -> crystal                php70 -> php@7.0                       saltstack -> salt
    geth -> ethereum                       php71 -> php@7.1                       wpcli-completion -> wp-cli-completion
    latexila -> gnome-latex                php72 -> php
    ==> Deleted Formulae
    arm                          ghc@8.0                      llvm@3.8                     python3
    artifactory-cli-go           gnupg@2.0                    luciddb                      root@5
    aws-cloudsearch              go@1.6                       mal4s                        ruby@1.9
    bokken                       go@1.7                       mimetic                      ruby@2.1
    boot2docker                  gpg-agent                    monotone                     tomcat@8.0
    boot2docker-completion       i3                           nazghul                      ufoai
    dirmngr                      i3status                     node@4                       voltdb
    ecj                          llvm@3.7                     picolisp                     wry
    
    ==> Downloading https://homebrew.bintray.com/bottles/boost-1.67.0_1.high_sierra.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring boost-1.67.0_1.high_sierra.bottle.tar.gz
    Error: The `brew link` step did not complete successfully
    The formula built, but is not symlinked into /usr/local
    Could not symlink include/boost/accumulators/accumulators.hpp
    Target /usr/local/include/boost/accumulators/accumulators.hpp
    is a symlink belonging to boost. You can unlink it:
      brew unlink boost
    
    To force the link and overwrite all conflicting files:
      brew link --overwrite boost
    
    To list all files that would be deleted:
      brew link --overwrite --dry-run boost
    
    Possible conflicting files are:
    /usr/local/include/boost/accumulators/accumulators.hpp -> /usr/local/Cellar/boost/1.60.0_2/include/boost/accumulators/accumulators.hpp
    /usr/local/include/boost/accumulators/accumulators_fwd.hpp -> /usr/local/Cellar/boost/1.60.0_2/include/boost/accumulators/accumulators_fwd.hpp
    /usr/local/include/boost/accumulators/framework/accumulator_base.hpp -> /usr/local/Cellar/boost/1.60.0_2/include/boost/accumulators/framework/accumulator_base.hpp
    ...
    ==> Summary
    🍺  /usr/local/Cellar/boost/1.67.0_1: 13,506 files, 450.9MB
    
  • 相关阅读:
    LeetCode—<动态规划专项>剑指 Offer 19、49、60
    Nginx基础组件的使用
    kafka的基本介绍【博学谷学习记录】
    【Linux常用(实用)命令大全】
    设计模式学习(十一):组合模式
    【面试题】说说JS中的this指向问题
    leetCode 746. 使用最小花费爬楼梯
    如何对Spring MVC中的Controller进行单元测试
    配置VUE环境过程中 npm报错的处理方案以及VUE环境搭建过程
    Big Data -- Postgres
  • 原文地址:https://blog.csdn.net/sjw890821sjw/article/details/140999464