Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hballington12/615f43c403ed1f5d5de2bb2e9b8f44ed to your computer and use it in GitHub Desktop.
Save hballington12/615f43c403ed1f5d5de2bb2e9b8f44ed to your computer and use it in GitHub Desktop.
How to Set Up Intel Fortran Development on Windows in Visual Studio & Visual Studio Code

How to Set Up Intel Fortran Development on Windows in Visual Studio & Visual Studio Code

What this guide is for: This post details how to set up a Fortran development environment in Windows using Visual Studio Code (VS-Code) and the Intel Fortran compiler, ifort.

Installations

We will be installing both Visual Studio as well as VS-Code. This is because we will be bootstrapping the debugger from Visual Studio for our development in VS-Code. We will also be installing the Intel Fortran compilers via the Intel oneAPI basekit and HPC toolkit.

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
  • Now restart Windows PowerShell with admin rights and run choco install make.

Useful Checks

  • To check that the Intel Fortran compiliers have been successfully integrated with Visual Studio, first launch Visual Studio. Then, choose "Create a new project". If the drop-down menu for language types contains an entry for Fortran, you should be good to go. image
  • In the start menu, search for and run "Intel oneAPI command prompt for IA32 for Visual Studio 2022". If all goes well, this should open a command prompt and initialise the environment. image

Setting up VS-Code

Now that the installations have been completed, we will be setting up the VS-Code environment. First we will install a few extensions for Fortran development. Second, we will bootstrap the Intel oneAPI command prompt to the VS-Code terminal. Third, we will configure the debugger to enable breakpoints support etc.

Extensions

Install the following extensions via the extensions tab on the left-hand pane: C/C++, Code Runner, Modern Fortran image

Terminal

From the toolbar, select Terminal -> New Terminal. By default, this opens a Powershell terminal. We wish to change this to the oneAPI command prompt. To do this, first select the drop-down in the terminal pane and choose Select Default Profile. image

This opens a prompt to choose one of the existing terminal setups. image

Click the ⚙️ icon next to Command Prompt to make a new terminal profile based on the existing setup for the Command Prompt. Name it "OneAPI" and press Enter. From the toolbar, use File -> Open File... to open the settings.json file. This is usually located at: C:\Users\<username>\AppData\Roaming\Code\User\settings.json. image

In the OneAPI args field, add: "/k \"Program Files (x86)\\Intel\\oneAPI\\setvars.bat". You can also edit the terminal icon colour and choose to override the name, but this is optional. Also add "terminal.integrated.defaultProfile.windows": "OneAPI" to set it as the default terminal. image

Now when you choose Terminal -> New Terminal, the OneAPI terminal we just made should run. It should also prompt the batch script setvars.bat to initialise the environment. image

At this point, you should be able to compile files in the terminal with ifort. Note that because we are on windows, the output files always have the extension:.exe. For example, running: ifort test.f90 -o test.o would compile test.f90 and make an exectuable file test.exe, which is a bit misleading. You could then run the exectuable with test.exe in the terminal. However, we have yet to take advantage of power of using VS-Code with Modern Fortran Extension. See below for more details...

Modern Fortran

By default, the Modern Fortran extension linter is set up for the gfortran compiler. To remedy this, we need to add 2 lines to our settings.json file:

  • "fortran.linter.compiler":"ifort" sets the default linter compiler to ifort.
  • fortran.linter.compilerPath": "C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\bin\\intel64\\ifort" provides the path to the ifort configuration file hidden away in the program files. image

With these additions, any files with the supported Fortran file extensions will be prescanned for syntax errors, warnings, etc. Modern Fortran uses the fortls language server for further improvements but I run into errors when trying to use this. Any assistance with this would be appreciated.

Debugging

The main reason for installing Visual Studio is for the debugger that comes bundled with it. We will bootstrap the debugger to an extension that can be read by the cppvsdbg debugger in VS-Code. Credit goes to this post.

  • Navigate to: C:\Program Files (x86)\Intel\oneAPI\debugger\latest\ide_support\fee\vs2022 and extract the FEE.VSIX_v17.vsix ZIP file to a directory of your choice.
  • In the newly-created directory, create a file called .vsdbg-config.json and paste inside it the following:
{
  "$schema": "https://aka.ms/vs/vsdbg-config-schema",
  "languages": [
    {
      "languageId": "{8e546c6f-4819-4dde-83dd-f998d52e6f33}",
      "vendorId": "{2a333b19-f91e-477b-8032-22de549d925a}",
      "name": "Fortran",
      "codeViewCompilerIds": [ { "code": 2 } ]
    }
  ]
}
  • Go back to VS-Code and open a project of your choice. Open the debug panel from the left-hand pane and select create a launch .json file. Then paste the following:
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
      {
        "name": "(Visual Studio Debugger) Fortran",
        "type": "cppvsdbg",
        "request": "launch",
        "program": "${workspaceFolder}/abt.exe",
        "args": [], // Possible input args for abt.exe
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": []
      }
    ]
  }
  • Finally create a file .cppvsdbg/extensions/FEE.link and inside it paste a single line containing the path to your chosen directory. Save this file in the working directory of your project.
  • If you compile with flag -debug:full, breakpoints should now function inside debug mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment