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 使用动态库
- 在Build Setings - Library Search Paths 增加lib文件目录
- 将lib文件目录中的libboost_regex.dylib文件拖入项目
- 确保在Build Phases - Link Bindary With Libraries中已经有该库
- 在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 -> [email protected] rebar@3 -> rebar3
crystal-lang -> crystal php70 -> [email protected] saltstack -> salt
geth -> ethereum php71 -> [email protected] wpcli-completion -> wp-cli-completion
latexila -> gnome-latex php72 -> php
==> Deleted Formulae
arm [email protected] [email protected] python3
artifactory-cli-go [email protected] luciddb root@5
aws-cloudsearch [email protected] mal4s [email protected]
bokken [email protected] mimetic [email protected]
boot2docker gpg-agent monotone [email protected]
boot2docker-completion i3 nazghul ufoai
dirmngr i3status node@4 voltdb
ecj [email protected] 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
标签:MacOS,lib,boost,local,accumulators,Boost,usr,include,安装
From: https://blog.csdn.net/sjw890821sjw/article/details/140999464