Intel Fortran Compiler + VScode での環境構築

Intel Fortran Compiler + VScode での環境構築の手順の覚え書き

Intel Fortran Compiler のインストール

あらかじめ,Visual Studio 2017 または 2019 を「C++ によるデスクトップ開発」を含んだ状態でインストールしておく.

Intel のダウンロードページから,Intel Fortran Compiler Classic and Intel Fortran Compiler for Windows のインストローラをダウンロードする.

インストローラを実行することで,コンパイラをインストールすることができる.

まず,”Installer will check your system requirements and launch automatically. Click continue to begin.” と出るので “Continue” をクリックする.

“I accept terms of the license agreement” にチェックを入れ,Custom Installation の “Customize” をクリックする.

Select Components で,Intel Fortran Compiler & Intel Fortran Compiler Classic にチェックが付いた状態で次へ.

Integrate IDE では,Microsoft Visual Studio のチェックをすべて外して次へ.

Software Improvement Program は好きな方をチェックして “Install” をクリックする.

VScode の設定

拡張機能 Modern Fortran の導入

VScode には,拡張機能 Modern Fortran を導入する.
設定で “Fortran > Linter: Compiler” を ifort とする.

環境変数の設定とVScodeを開くバッチファイルの作成

Fortran のソースファイルのあるディレクトリに次のようなバッチファイルを作成し,VScode を開くときはこのバッチファイルを実行することで開くようにする.

@echo off
rem ifort を実行する上での環境変数の設定
call "C:\Program Files (x86)\Intel\oneAPI\setvars.bat"
rem 装置番号を設定するならばここでする.
rem set fort11=hoge.dat
code .

tasks.json の設定

Fortran のソースファイルのあるディレクトリにディレクトリ .vscode を,その下に tasks.json を作成する.

tasks.json の記述内容は以下の通り.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build using intel fortran",
            "type": "shell",
            "options": {
                "shell": {
                    "executable": "${env:windir}\\sysnative\\cmd.exe",
                    "args": ["/c"]
                }
            },
            "command": "ifort",
            "args":[
                "/debug:full",
                "${fileBasename}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            }
        }
    ]
}

これにより,VScode で Fortran のデバッグ用コンパイルができるようになる.
(ifort でデバッグ用コンパイルするオプションは,ifort /debug:full hoge.f90 である.)

Ctrl+Shift+P でコマンドパレットを開き,”Tasks: Tun Task” > “build using intel fortran” でコンパイルされる.

なお,task.json を記述する上で使用可能な変数は,Variables Reference を参照のこと.

launch.json の設定

tasks.json と同様に .vscode 下に launch.json を作成する.

{
    "configurations": [
        {
            "name": "(Windows) 起動",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal"
        }
    ]
}

これにより,VScode 上で Fortran のデバッグが可能となる.

参考文献