Boost 1_86 のインストール(ソースコードを用いたインストール)(Build Tools for Visual Studio を使用)と使用例(Windows 上)

Windows上でBoostライブラリをインストールする手順を説明する.最初,c:\boostディレクトリにGitを使用してBoostをクローンする.次に Boost のビルドツールを使用してコンパイルとインストールを行う.インストール後に,テスト実行を行う.システム環境変数を設定してBoostの使用準備を整える.

目次

関連する外部ページ

Boost の利用条件などは、利用者が確認すること。次のWeb ページを活用してください

https://www.boost.org/users/license.html

前準備

Build Tools for Visual Studio 2022 のインストール(Windows 上)

Build Tools for Visual Studio は,Visual Studio の IDE を含まない C/C++ コンパイラ,ライブラリ,ビルドツール等のコマンドライン向け開発ツールセットである。

以下のコマンドを管理者権限コマンドプロンプトで実行する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。

REM VC++ ランタイム
winget install --scope machine --id Microsoft.VCRedist.2015+.x64 -e --silent --disable-interactivity --force --accept-source-agreements --accept-package-agreements --override "/quiet /norestart"

REM Build Tools + Desktop development with C++(VCTools)+ 追加コンポーネント(一括)
winget install --id Microsoft.VisualStudio.2022.BuildTools --accept-source-agreements --accept-package-agreements ^
    --override "--passive --wait --norestart --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --add Microsoft.VisualStudio.Component.VC.Llvm.Clang --add Microsoft.VisualStudio.ComponentGroup.ClangCL --add Microsoft.VisualStudio.Component.VC.CMake.Project --add Microsoft.VisualStudio.Component.Windows11SDK.26100"

--add で追加されるコンポーネント

上記のコマンドでは,まず Build Tools 本体と Visual C++ 再頒布可能パッケージをインストールし,次に setup.exe を用いて以下のコンポーネントを追加している。

インストール完了の確認

winget list Microsoft.VisualStudio.2022.BuildTools

上記以外の追加のコンポーネントが必要になった場合は Visual Studio Installer で個別にインストールできる。

Visual Studio の機能を必要とする場合は、追加インストールできる。

Git のインストール(Windows 上) [クリックして展開]

管理者権限コマンドプロンプトで以下を実行する.管理者権限は,winget の --scope machine オプションでシステム全体にインストールするために必要となる.

REM Git をシステム領域にインストール
winget install --scope machine --id Git.Git -e --silent --disable-interactivity --force --accept-source-agreements --accept-package-agreements --override "/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS=""icons,ext\reg\shellhere,assoc,assoc_sh"" /o:PathOption=Cmd /o:CRLFOption=CRLFCommitAsIs /o:BashTerminalOption=MinTTY /o:DefaultBranchOption=main /o:EditorOption=VIM /o:SSHOption=OpenSSH /o:UseCredentialManager=Enabled /o:PerformanceTweaksFSCache=Enabled /o:EnableSymlinks=Disabled /o:EnableFSMonitor=Disabled"

Boost のインストール(Build Tools for Visual Studio を利用)(Windows 上)

前もって,Boost をインストールするディレクトリを決めておく

このページでは,c:\boost 下にインストールするものとして説明する.

  1. 以下の操作を管理者権限コマンドプロンプトで実行する (手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。

  2. インストール

    c:\boost 下にインストールするものとして説明する.

    cd c:\
    rmdir /s /q boost
    git clone --recursive https://github.com/boostorg/boost
    cd boost
    .\bootstrap.bat
    .\b2.exe --prefix=build --build-type=complete toolset=msvc link=static,shared address-model=64 install
    
  3. 終了の確認

    エラーメッセージが出ていないこと。

  4. バージョンとインクルードディレクトリの確認

    次のコマンドで確認する.ここで確認したことは,のちほど,使用する.

    dir "c:\boost\build\include"
    
  5. テスト実行

    以下のコマンドを管理者権限x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)で実行する   (手順:スタートメニュー →Visual Studio 20xx」の下の「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」 → 「管理者として実行」)。

    boost-1_86」のところは,先ほど確認したインクルードディレクトリに一致させること.

    cl /EHsc /I "c:\boost\build\include\boost-1_86" ^
        "c:\boost\libs\geometry\example\01_point_example.cpp" 
    .\01_point_example.exe
    del 01_point_example.exe
    
  6. Windowsシステム環境変数 BOOST_DIR に,c:/boost/build を設定

    次のコマンドを実行

    powershell -command "[System.Environment]::SetEnvironmentVariable(\"BOOST_DIR\", \"c:\boost\build\", \"Machine\")"
    
  7. Windowsシステム環境変数 BOOST_ROOT に,c:\boost\build を設定

    次のコマンドを実行

    powershell -command "[System.Environment]::SetEnvironmentVariable(\"BOOST_ROOT\", \"c:\boost\build\", \"Machine\")"
    
  8. Windowsシステム環境変数 Boost_INCLUDE_DIR に,C:\boost\build\include\boost-1_86 を設定

    次のコマンドを実行

    powershell -command "[System.Environment]::SetEnvironmentVariable(\"Boost_INCLUDE_DIR\", \"C:\boost\build\include\boost-1_86\", \"Machine\")"
    

サンプルプログラムの紹介

作者に感謝します.

http://www.boost.org/doc/libs/1_62_0/more/getting_started/windows.html#build-a-simple-program-using-boost のものを記載.

#include <stdio.h>
#include<boost/lambda/lambda.hpp>
#include<iostream>
#include<iterator>
#include<algorithm>

int main()
{
	using namespace boost::lambda;
	typedef std::istream_iterator<int> in;

	std::for_each(
		in(std::cin), in(), std::cout << (_1 * 3) << " ");
}
  1. 上のプログラムを hoge.cpp のようなファイル名で保存.(拡張子は .cpp)
  2. 次のコマンドにより,コンパイルして実行
    cl /EHsc /I "c:\boost\build\include\boost-1_86" hoge.cpp
    echo 1 2 3 | .\hoge.exe