flann 1.9.1 のインストール(ソースコードを用いたインストール)(Build Tools for Visual Studio を使用)(Windows 上)

FLANN(Fast Library for Approximate Nearest Neighbors)は,高次元空間での最近傍探索を行うライブラリである.Windows上でのインストール手順は以下の通り:事前にpkg-config,zlib,liblz4をインストールし,必要に応じてHDF5もインストールする.その後,FLANNのソースコードをGitHubからクローンし,cmakeを使用してビルド設定を生成.ソースコードをビルドしてインストールし,最後にシステム環境変数(FLANN_ROOT,LIB)を適切に設定する.

目次

flann の公式ページ:https://github.com/mariusmuja/flann

前準備

Build Tools for Visual Studio 2026(ビルドツール)のインストール

Build Tools for Visual Studio 2026(ビルドツール)のインストールを行い、C/C++ コードのビルド環境を整える。

[Build Tools for Visual Studio 2026(ビルドツール)のインストール手順を見るには、ここをクリック]

Windows での Build Tools for Visual Studio 2026 のインストール

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

以下のコマンドは、Build Tools が未インストールの場合は winget で新規インストールし、インストール済みの場合は setup.exe modify でコンポーネントを追加する(バージョンは変更しない)。

インストールコマンドの実行方法

管理者権限コマンドプロンプトを起動する(手順: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 ============================================================
REM Visual Studio Build Tools + Desktop development with C++
REM (VCTools、MSBuildTools、CMake連携、Clang、Windows 11 SDK)
REM ============================================================
REM 進行中のインストーラーを停止(ロック競合回避)
taskkill /F /IM vs_setup.exe /T >nul 2>&1
taskkill /F /IM vs_installer.exe /T >nul 2>&1
taskkill /F /IM vs_installerservice.exe /T >nul 2>&1

REM 未インストール時: winget で新規インストール
REM インストール済み時: setup.exe modify でコンポーネント追加(バージョンは変更しない)
winget list --id Microsoft.VisualStudio.BuildTools 2>nul | findstr /i "BuildTools" >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    for /f "usebackq delims=" %P in (`"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -products Microsoft.VisualStudio.Product.BuildTools -property installationPath`) do start /wait "" "C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe" modify --installPath "%P" --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Workload.MSBuildTools --add Microsoft.VisualStudio.Component.VC.CMake.Project --add Microsoft.VisualStudio.Component.VC.Llvm.Clang --add Microsoft.VisualStudio.Component.VC.Llvm.ClangToolset --add Microsoft.VisualStudio.Component.Windows11SDK.26100 --includeRecommended --quiet --norestart --nocache
) else (
    winget install --scope machine --id Microsoft.VisualStudio.BuildTools -e --silent --disable-interactivity --force --accept-source-agreements --accept-package-agreements --override "--quiet --wait --norestart --nocache --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --add Microsoft.VisualStudio.Workload.MSBuildTools --add Microsoft.VisualStudio.Component.VC.CMake.Project --add Microsoft.VisualStudio.Component.VC.Llvm.Clang --add Microsoft.VisualStudio.Component.VC.Llvm.ClangToolset --add Microsoft.VisualStudio.Component.Windows11SDK.26100"
)

REM 破損時の修復(任意、動作がおかしくなった場合)
REM "C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe" repair --installPath "C:\Program Files (x86)\Microsoft Visual Studio\18\BuildTools" --quiet --norestart

REM 導入確認(インストールパスが表示されれば正常)
"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -products * -requires Microsoft.VisualStudio.Workload.VCTools -property installationPath

上記のコマンドでは、Build Tools 本体と Visual C++ 再頒布可能パッケージをインストールし、続いて以下のコンポーネントを追加している。

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

インストール完了の確認

winget list Microsoft.VisualStudio.BuildTools

Visual Studio を必要とするとき

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"

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

管理者権限コマンドプロンプトで以下を実行する。管理者権限のコマンドプロンプトを起動するには、Windows キーまたはスタートメニューから「cmd」と入力し、表示された「コマンドプロンプト」を右クリックして「管理者として実行」を選択する。

REM CMake をシステム領域にインストール
winget install --scope machine --id Kitware.CMake -e --silent --disable-interactivity --force --accept-source-agreements --accept-package-agreements --override "/qn /norestart ADD_CMAKE_TO_PATH=System"

flann 1.9.1 のインストール(ソースコードを用いたインストール)(Build Tools for Visual Studio を使用)(Windows 上)

pkg-config のインストール

Windows での pkg-config のインストール: 別ページ »で説明

zlib のインストール

Windows での zlib のインストール: 別ページ »で説明

liblz4 のインストール

Windows での liblz4 のインストール: 別ページ »で説明

(オプション)hdf5 のインストール

必ずしも必要ではないが,必要になったときはインストールする.

Windows での hdf5 のインストール: 別ページ »で説明

flann のインストール

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

  2. flann 1.9.1 のソースコードのダウンロード

    試したところ 1.9.2 はうまくインストールできなかった. そのため,flann 1.9.1 のソースコードをダウンロードする.

    cd /d c:%HOMEPATH%
    rmdir /s /q flann
    git clone -b 1.9.1 https://github.com/mariusmuja/flann
    
  3. ソースコードの変更とcmake の実行
    cd /d c:%HOMEPATH%
    cd flann
    powershell -Command "$f=\"$env:HOMEDRIVE$env:HOMEPATH\flann\src\cpp\flann\util\heap.h\"; $lines=Get-Content $f; for($i=0;$i -lt $lines.Length;$i++){if($lines[$i] -match 'struct CompareT\s*:'){$lines[$i]='    struct CompareT'}}; $lines | Set-Content $f"
    rmdir /s /q build
    mkdir build
    cd build
    del CMakeCache.txt
    rmdir /s /q CMakeFiles
    cmake .. -A x64 -T host=x64 ^
        -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ^
        -DCMAKE_INSTALL_PREFIX="c:\flann" ^
        -DBUILD_EXAMPLES=ON ^
        -DBUILD_PYTHON_BINDINGS=OFF ^
        -DBUILD_MATLAB_BINDINGS=OFF
    
  4. cmake の実行結果の確認

    * 下の通りになるとは限らない.エラーメッセージが出るなど場合は,前で cmake を実行したときの設定を変えてやり直す

  5. ソースコードからビルドし、インストールする
    cmake --build . --config Release --target INSTALL -- /m
    
  6. 結果の確認

    エラーメッセージが出ていないことを確認.最後のほうに「0 エラー」のように表示されるのを確認.

    * 黄色の警告メッセージは無視しても良い

    * ここでエラーメッセージが出たときは、
  7. Windowsシステム環境変数 FLANN_ROOT に,c:\flann を設定

    次のコマンドを実行

    powershell -command "[System.Environment]::SetEnvironmentVariable(\"FLANN_ROOT\", \"c:\flann\", \"Machine\")"
    
  8. Windowsシステム環境変数 LIB の設定に,c:\flann\lib を追加
    powershell -command "$oldval = [System.Environment]::GetEnvironmentVariable(\"LIB\", \"Machine\"); $oldval += \";c:\flann\lib\"; [System.Environment]::SetEnvironmentVariable(\"LIB\", $oldval, \"Machine\")"
    

【まとめ】 依存ライブラリをインストール後,FLANNをクローン,cmakeでビルド,環境変数を設定.