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.
1 Like
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
I managed to get exactly what I wanted, for simple files with meson. Go to this page Build tools — Fortran Programming Language 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):
- Create a project folder
- 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
project('main', 'fortran', meson_version: '>=0.49')
executable('main', files('main.f90', 'functions.f90'))
- 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
- Inside the
.vscode
folder, paste the following in a json file called launch.json
{
"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.
2 Likes