在 VSCode Fortran 调试中检查从另一个模块导入的变量

Inspect variable imported from another module in VSCode Fortran debugging

我正在调试一些包含许多 Fortran 模块的代码,其中一些模块彼此共享变量。不幸的是,带有 VScode 的 gdb 在调试时似乎无法检查导入的变量。

目前,当我需要检查导入变量时,唯一的方法是停止调试,并手动更改代码以包含一个与导入变量相等的局部变量。在下面的示例中,要找出 foo%bar 传递给函数 a_function 的值,我必须声明一个新变量,例如

module setup
  type(customDerived) :: foo
  foo%bar = 1
end module setup

module example
  use setup, only: foo
  integer(ik) :: foobar    <-- Stop debugging, add these lines, restart and inspect 'foobar'
  foobar = foo%bar         <--
  a_function(foo%bar)
end module example


这显然很耗时,我不知道为什么VSCode应该不能检查全局变量。有任何想法吗?以下是我目前在makefile中打开的gfortran编译器标志

-Og -g -Wall -Wextra -Wline-truncation -pedantic -fimplicit-none -fcheck=all -fbacktrace

此问题已部分处理:Fortran module variables not accessible in debuggers。 基本上,在 Visual Studio 代码的 WATCH 面板中,您可以“添加表达式”并使用语法 module::variable.

监视模块变量
  • 不太确定您是否可以使用这种语法轻松地观察派生类型。
  • 如果 variable 是一个(例如 2d)数组,您可以使用像 module::variable(12,457)
  • 这样的常用 Fortran 索引单独访问它的元素
  • 可以通过 VS Code 的“调试控制台”中的 gdb 查询访问数组的多个元素。使用-exec前缀传递gdb指令,如:-exec p module::variable@100显示module::variable的前100个元素。
  • 通过指定起始索引,可以在“监视”面板中显示多个元素,如下所示:module::variable(1,1)@100.

有用的资源:https://numericalnoob.blogspot.com/2012/08/fortran-allocatable-arrays-and-pointers.html 当然还有https://www.gnu.org/software/gdb/documentation/