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
.
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.
- First, install Visual Studio 2022 Community edition: https://visualstudio.microsoft.com/vs/community/
- During installation, check the box for
Desktop development with C++
.
- During installation, check the box for
- Second, install the Intel oneAPI base toolkit: https://www.intel.com/content/www/us/en/developer/tools/oneapi/base-toolkit-download.html
- Choose Windows and select the online installer.
- Note: signing in is not required to download.
- Continue with the recommended installation.
- You should have the option to enable integration with Visual Studio 2022. Make sure this is enabled.
- Third, install the Intel oneAPI HPC toolkit: https://www.intel.com/content/www/us/en/developer/tools/oneapi/hpc-toolkit-download.html
- Repeat with similar steps to installing the base toolkit, ensuring that integration with Visual Studio 2022 is enabled.
- Fourth, install VS-Code: https://code.visualstudio.com/download
- Fifth, we will install
chocolatey
, which will then be used to installmake
. Search forWindows PowerShell
in the start menu and chooseRun as Adminstrator
. Run the following command to installchocolatey
:
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 runchoco install make
.
- 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.
- 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.
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.
Install the following extensions via the extensions tab on the left-hand pane: C/C++
, Code Runner
, Modern Fortran
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
.
This opens a prompt to choose one of the existing terminal setups.
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
.
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.
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.
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...
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 toifort
.fortran.linter.compilerPath": "C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\bin\\intel64\\ifort"
provides the path to theifort
configuration file hidden away in the program files.
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.
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 theFEE.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.