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
相关推荐
我爱cope25 分钟前
【Agent智能体4 | 智能体AI的应用】
数据库·人工智能·职场和发展
h_a_o777oah1 小时前
状态机+划分型 DP :深度解析K-划分问题下 DP 状态的转移逻辑(洛谷P2679 P2331 附C++代码)
c++·算法·动态规划·acm·状态机dp·划分型dp·滚动数组优化
一个人旅程~1 小时前
如何避免在使用win安装U盘启动macbook时候出现键盘触摸板卡死的问题
windows·经验分享·macos·电脑
雪度娃娃3 小时前
Asio异步读写——连接的安全回收问题
开发语言·c++·安全·php
不吃土豆的马铃薯3 小时前
Spdlog 进阶:日志基本控制、日志格式控制、异步记录器
linux·服务器·开发语言·前端·c++
liulilittle3 小时前
TCP UCP:基于卡尔曼滤波的BBR增强型拥塞控制算法
linux·网络·c++·tcp/ip·算法·c·通讯
过期动态3 小时前
【LeetCode 热题 100】字母异位分组
java·算法·leetcode·职场和发展·哈希算法
咩咦4 小时前
C++学习笔记26:static 静态成员
c++·学习笔记·static·静态成员变量·静态成员·静态成员函数
秋落风声4 小时前
内存池仿Nginx C++实现
c++·nginx
小白要努力sgy4 小时前
实时通信框架CyberRT
c++·自动驾驶·实时通讯