VS Code Fortran configuration

VSFortran Despite Fortran being an ancient language, its still comes with its own set of advantages. Recently, while debugging Abaqus subroutines, I have to repeatedly running code for testing. Although the integration with Abaqus is necessary for running the entire code, the individual modules can still be tested separately. The best method for testing modules is to run them as small, standalone programs. However, VS Code does not seem to support running Fortran code directly. A useful plugin for this purpose is Code Runner, but unfortunately, it doesn’t support Fortran by default and requires some adjustments. First, let’s start with a test code.

Test code

Next, go to the plugin page and open settings, then type “code runner” into the search box,

Settings

Set the default language in Code-runner “Default Language” setting to Fortran. Then, in Code-runner “Executor Map”, add the following configuration. It defaults to using the gfortran compiler, which we need to download and install from the official website if we haven’t already,

1
2
3
4
5
6
7
8
9
{
    "workbench.colorTheme": "Default Light+",
    "fortran.gfortranExecutable": "/usr/local/bin/gfortran",
    "code-runner.executorMap": {
        "fortran": "cd \$dir && gfortran \$fileName -o \$fileNameWithoutExt && \$dir\$fileNameWithoutExt"
    },
    "code-runner.saveFileBeforeRun": true,
    "code-runner.defaultLanguage": "fortran"
}

Settings

After completing these steps, we can directly run the code by clicking the button in the upper right corner of the program interface,

VScode

If you’re looking to configure Notepad++ to run Fortran code, you can refer to my previous post on running OpenSees with Notepad++. Simply modify the instructions for running OpenSees to running Fortran code. Replace the code segment with the following script,

1
2
3
npp_save
cd $(CURRENT_DIRECTORY)
gfortran "$(FULL_CURRENT_PATH)"