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

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

#include <iostream>
#include <boost/regex.hpp>
 
int main(int argc, const char * argv[]) {
    std::string   str = "2013-08-15";
    boost::regex  rex("(?<year>[0-9]{4}).*(?<month>[0-9]{2}).*(?<day>[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
相关推荐
Charles Ray26 分钟前
C++学习笔记 —— 内存分配 new
c++·笔记·学习
重生之我在20年代敲代码27 分钟前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
GEEKVIP4 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
迷迭所归处6 小时前
C++ —— 关于vector
开发语言·c++·算法
CV工程师小林7 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
white__ice7 小时前
2024.9.19
c++
天玑y7 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
自陈8 小时前
蓝桥杯嵌入式客观题合集
蓝桥杯·蓝桥杯嵌入式客观题
姜太公钓鲸2338 小时前
c++ static(详解)
开发语言·c++
菜菜想进步8 小时前
内存管理(C++版)
c语言·开发语言·c++