How to set up tasks.json file in VSCODE to compile Fortran programs?

Viewed 776

I want to set up VScode (OS: Windows 10) to create and then compile programs written in Fortran 90/95. I can do this by typing in the terminal : gfortran -o Example_exe Example.f90 and then ./Example_exe. I don't want to have to write these lines every time, so I tried to set up my tasks.json file to automate a build routine using gfortran as compiler. I found this tutorial : https://titanwolf.org/Network/Articles/Article?AID=360e0bde-0507-4de4-960c-2eae8fa8c782#gsc.tab=0 but the tasks.json file given is unclear.

Can I have a tasks.json file setup to automate my build routine please ?

I have installed the following extensions : Modern Fortran, Fortran IntelliSense, Code Runner, Fortran Breakpoint Support

1 Answers

Yes you can. Assuming that you want to execute in debug mode, you should create a tasks.json and a launcher.json and place them in .vscode/ at the root of your workspace. Assuming the following file structure and a debugging mode with GDB for the execution:

root/

  • .vscode/
  • code/
    • Example_exe

This is what your tasks.json should look like:

{
    "version": "2.0.0",
    "tasks": [

        {
            "label": "compile",
            "type": "shell",
            "command": "gfortran -o Example_exe Example.f90 -g",
            "options": {
                "cwd": "code/"
            }
        }
    ]

}

And then, the launch.json, which will identify tasks.json as a "preLaunchTask".

{
    "version": "0.2.0",
    "configurations":[
        {
            "name": "Run my example",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\code\\example_exe.exe",
            "args": ["],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}\\code",
            "miDebuggerPath": "gdb.exe",
            "preLaunchTask": "compile",

        }
    ]
}

Launch the debugger by pressing F5 (or in the Run menu).

If you don't want to run in debug mode, have a look at this issue.

Sources:

Related