# Interactive debugging with with VSCode and Modern Fortran

**URL:** <https://fortran-lang.discourse.group/t/interactive-debugging-with-with-vscode-and-modern-fortran/9432>\
**Category:** Help\
**Created:** [March 22, 2025, 12:00am UTC](https://fortran-lang.discourse.group/t/interactive-debugging-with-with-vscode-and-modern-fortran/9432 "2025-03-22T00:00:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sideshowbill](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Sideshowbill](https://fortran-lang.discourse.group/u/Sideshowbill)\
**Post date:** [March 22, 2025, 12:00am UTC](https://fortran-lang.discourse.group/t/interactive-debugging-with-with-vscode-and-modern-fortran/9432/1 "2025-03-22T00:00:03Z")

</div>

Hi, second post here.

I’ve been using `fpm` since “retrying” Fortran lately. I have a mechanical engineering background and I’m not used to cli debugging with gdb. I learned programming with Matlab and got quite used to the interactive debugging that comes with it.

I’ve checked the documentation of `Modern Fortran` and apparently I can build a json file that will be used for the debug configuration. However, I don’t think it will work with projects made with `fpm`.

My question is: to experienced programmers, what is your typical workflow/setup to debug Fortran code using available tools in VSCode. I wish there was something similar to Rust with rust-analyzer and cargo.

---

<div class="post-metadata">

**Author:** ![aledinola](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@aledinola](https://fortran-lang.discourse.group/u/aledinola)\
**Post date:** [March 22, 2025, 1:44pm UTC](https://fortran-lang.discourse.group/t/interactive-debugging-with-with-vscode-and-modern-fortran/9432/2 "2025-03-22T13:44:25Z")

</div>

That’s a very important question, thanks for asking. I have a background similar to yours and I never managed to debug Fortran code in VScode. Other than that, VScode and Modern Fortran are great tools of course.

I think what’s missing is a step-by-step guide for dummies (i.e. people who don’t know what pip means) on how to set up interactive debugging for Fortran in VSCode with Modern Fortran. There are many researchers who use Fortran (in econ, physics, etc) but don’t have a CS background and would find this very useful

---

<div class="post-metadata">

**Author:** ![Sideshowbill](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Sideshowbill](https://fortran-lang.discourse.group/u/Sideshowbill)\
**Post date:** [March 22, 2025, 11:27pm UTC](https://fortran-lang.discourse.group/t/interactive-debugging-with-with-vscode-and-modern-fortran/9432/3 "2025-03-22T23:27:33Z")

</div>

I managed to get exactly what I wanted, for simple files with meson. Go to this page [Build tools — Fortran Programming Language](https://fortran-lang.org/learn/building_programs/build_tools/) and get meson running. Do the simple tutorial and you should be able to do my example.

You should install these extensions (Besides Modern Fortran and Fortls):

- C/C++ (You most likely have it from using Modern Fortran)
- Meson - language support for Visual Studio Code

Here are the steps (I use Ubuntu on WSL but I guess it is similar on Windows):

1. Create a project folder
2. Create a `meson.build` file to build your program. Here is a simple one to build a program where the `main` and `functions` files are in the same directory

```auto
project('main', 'fortran', meson_version: '>=0.49')
executable('main', files('main.f90', 'functions.f90'))

```

1. Now, meson should ask if you want to setup the project. Click yes. Or use the command `meson setup builddir`. There should be two new folders : `.vscode` and `builddir`
2. Inside the `.vscode` folder, paste the following in a json file called `launch.json`

```auto
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Fortran",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/builddir/main", // Change executable name if needed
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb", // Adjust if your GDB is in a different location
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

```

Now, you should be able to use the debug tab on the left and debug your Fortran program by clicking on `Debug Fortran`. Just put breakpoints where you need it. Try it with a simple program from the meson examples in the page quoted above.

Let me know if that works for you. Hope it helps.
