【问题标题】:Inspect variable imported from another module in VSCode Fortran debugging在 VSCode Fortran 调试中检查从另一个模块导入的变量
【发布时间】:2021-07-07 14:31:23
【问题描述】:

我正在调试一些包含许多 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

【问题讨论】:

    标签: visual-studio-code fortran gdb gfortran vscode-debugger


    【解决方案1】:

    此问题已在Fortran module variables not accessible in debuggers 中得到部分处理。 基本上,在 Visual Studio Code 的 WATCH 面板中,您可以“添加表达式”并使用语法 module::variable 监视模块变量。

    • 不太确定是否可以使用这种语法轻松监视派生类型。
    • 如果 variable 是一个(例如 2d)数组,您可以使用通常的 fortran 索引(如 module::variable(12,457))单独访问其元素
    • 可以通过 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/

    【讨论】:

    • 像魅力一样工作,谢谢!
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多