C++:Boost库
今日安装一个PSI库时,需要boost库,在此认识一下boost库,转载:macOS 中Boost的安装和使用
Boost是一个功能强大、构造精巧、跨平台、开源并且完全免费的C程序库,有着“C‘准’标准库”的美誉,值得每位C++程序员学习使用。 ##安装Boost ###使用源码安装 (1)下载Boost源码 (2)解压放在任意目录,例如/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查看使用文档
###使用Homebrew安装 (1)下载安装HomeBrew
brew install boost
(2)留意运行日志会显示头文件目录 /usr/local/Cellar/boost/1.60.0_2/include, lib目录/usr/local/Cellar/boost/1.60.0_2/lib ###使用MacPort安装 下载安装MacPort
sudo port install boost
##在CLion中使用Boost (1)新建一个C++项目 (2)在cmakelists中 增加头文件目录
include_directories(/Users/pam/Desktop/pam/boost_1_78_0/)
(3)替换main.cpp中代码,运行!输入任意数字回车可看到结果。
##在XCode项目中使用Boost (1)新建一个Command Line Tool项目 (2)在Build Setings - Header Search Paths 增加头文件目录 (3)替换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; }
##在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文件目录。 ##使用动态库 (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
- Boost库中宏BOOST_AUTO与C++新特性--auto关键字
- c++ Boost库简介
- C++ boost库----share_from_this类的作用和实现原理
- [置顶] 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- C++Boost库学习之timer库
- C++Boost库介绍
- 使用Boost库中的组件进行C++内存管理
- c++ Boost库之boost::bind学习
- 【c++】模拟实现boost库下的scoped_array
- C++ boost库无锁队列多线程并行测试与编译方法
- poco c++和Boost库的对比分析
- Linux下C++程序链接boost库
- c++的boost库学习笔记
- C++ Boost库学习
- C++风格_Boost库
- 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- Mac上编译安装运行c++ boost库简单步骤
- c++ boost库在VS2012下的安装
- EPD中已自带Mingw,如何安装c++ boost库?
- 【转】C++ Boost库简介