MacOS 中Boost的安装和使用
创始人
2024-11-03 20:38:25
0

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 

相关内容

热门资讯

微信炸金花房卡有没有购买/微信... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
终于找到“微信炸金花链接在哪里... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
微信炸金花房卡链接在哪弄的/怎... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
正版授权“微信牛牛房卡客服微信... 九酷大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
微信玩链接炸金花房卡/战皇大厅... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
买房卡的链接炸金花房卡/微信斗... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
一分钟了解“牛牛房卡哪里有卖的... 起点大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
微信群牛牛房卡怎么买/新九天大... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享受...
房卡必备教程“微信牛牛房卡在哪... 新全游牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡...
微信群链接拼三张房卡/新518... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
一分钟推荐“炸金花房卡链接怎么... 金牛座金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
微信里面炸金花房卡在哪买/新蓝... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
一分钟了解“哪里有卖微信炸金花... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
在哪里买斗牛微信房卡/茄子娱乐... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享受...
正版授权“牛牛房卡哪里有卖的”... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
炸金花房卡链接在哪弄的/牛牛房... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
微信炸金花房卡如何购买/悟空大... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
一分钟了解“炸金花房卡专卖店联... 新神盾是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享...
微信炸金花房间怎么弄/新皇豪大... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
ia实测“微信金花群怎么买房卡... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...