首页 > 其他分享 >MacOS 中Boost的安装和使用

MacOS 中Boost的安装和使用

时间:2024-08-07 19:23:40浏览次数:11  
标签:MacOS lib boost local accumulators Boost usr include 安装

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 -> [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

相关文章

  • 植物大战僵尸免安装版 汉化版
    游戏简介:《植物大战僵尸》(Plantsvs.Zombies,简称PVZ)是由PopCapGames发售的一款益智策略类塔防游戏。玩家通过武装多种植物切换不同的功能,快速有效地把僵尸阻挡在入侵的道路上。不同的敌人,不同的玩法构成五种不同的游戏模式,加之黑夜、浓雾以及泳池之类的障碍增加了游戏挑战......
  • Linux 环境下为VirtualBox安装增强功能
    csdn搬家VirtualBox安装CentOS后,再安装增强功能就可以共享文件夹、粘贴板以及鼠标无缝移动,主要步骤如下:1、yum-yupdate2、yum-yinstallg++gccgcc-c++makekernel-*#主要是在安装增强工具提示没有安装这些软件3、yum-yinstallbzip2*......
  • SVN服务器安装流程说明
    一、准备软件:Apache2.2.6、Subversion1.6.16、TortoiseSVN1.6.7,此次安装版本如下: 二、安装步骤:1.安装Apache2.2(默认安装即可)64位系统安装目录默认为:C:\ProgramFiles(x86)\ApacheSoftwareFoundation\Apache2.22. 确认Apache是否正确安装打开浏览器,输入htt......
  • 使用ansible安装mongodb分片集群
    【说明】使用ansible安装一个分片集群,三台服务器,三个mongos,三个config,三个分片节点,每三个分片有三个副本(每个节点运行三个端口的mongod)  [mongo_servers]10.x.x.21ansible_user=rootansible_ssh_pass=xxxxxxxxcluster_role=mongo1......
  • Linux系统安装ComfyUI
    环境:Ubuntu22.04.1LTS+  NVIDIAGeForceRTX3090一、安装显卡驱动 访问NVIDIA官方网站(https://developer.nvidia.com/cuda-downloads)获取对应安装命令https://developer.nvidia.com/cuda-downloads安装完成后执行nvidia-smi检测安装是否成功二、安装Miniconda......
  • CentOS Stream 9 安装mysql 开启远程访问 忽略大小写
     更新sudodnfupdate安装MySQL服务器:这边安装的是默认8.0sudodnfinstallmysql-server启动MySQL服务:sudosystemctlstartmysqld确保MySQL服务设置为在启动时自动启动:sudosystemctlenablemysqld运行初始安全脚本来设置root用户密码和调整安全......
  • RockyLinux安装Docker
    更新系统sudodnfupdate安装 Docker 所需的软件包和依赖项sudodnfinstall-yyum-utilsdevice-mapper-persistent-datalvm2添加Docker官方的YUM仓库#官方sudoyum-config-manager--add-repohttps://download.docker.com/linux/centos/docker-ce.repo#阿......
  • keepalive离线安装-解决openssl-devel安装问题
    keepalive源码安装1.下载源码包(这里我是用最新的2.3.1版本):官网2.解压安装包执行:#prefix后边的路径为keepalived安装的路径$./configure--prefix=/usr/local/keepalived$make$makeinstall3.安装openssl-devel如果遇到错误:(没有错误则不用管这一步)configure......
  • Navicat Premium 17 解锁版下载及安装教程 (数据库管理工具)
    前言NavicatPremium是一套可创建多个连接的数据库开发工具,让你从单一应用程序中同时连接MySQL、MariaDB、MongoDB、SQLServer、Oracle、PostgreSQL和SQLite。它与OceanBase数据库及AmazonRDS、AmazonAurora、AmazonRedshift、MicrosoftAzure、OracleCloud、......
  • Python安装教程(含MacOS&&Linux系统)
    Python安装教程Windows用户访问Python官网:WelcometoPython.org 打开下载好的安装包根据提示安装   Pip换源(系统级别)(注:Pip在3.4以上的版本才支持,3.4之前的版本可以在cmd中输入 easy_installpip 下载pip)1.为什么要换源?Python安装......