有没有办法像在Excel中使用VBA那样“逐步执行”Fortran代码?
我有一些Fortran源代码,我可以理解一般背后思想,但我以前从未使用过 Fortran,所以我想确切地了解每行执行时发生的情况(就像您可以在 Excel 中通过逐步执行 VBA 一样一行一行地编写代码并观察变量和数组在代码中的任何一点都有什么值)。
有没有办法通过某种用户界面来单步执行源代码,以便我可以准确地看到定义了哪些变量,它们采用了哪些值等等?
对于某些情况:我在科学和工程领域工作,但编码通常不是我工作的重要组成部分(您可能从我的问题内容中可以看出),我通常只处理 VBA 中的简单脚本来操作数据。我有 Fortran 代码的编译版本,它工作正常,但我知道我需要修改源代码并为我的目的重新编译它。不幸的是,编写原始代码的人无法提供建议/输入。另一个注意事项:我不确定如何判断使用的是哪个版本的 Fortran...
谢谢!
回答
任何受人尊敬的调试器都将允许您单步执行代码。下面是一个简单的例子,显示了一个gdb, 正在使用中。要点:
- 确保编译并链接到
-g标志 run运行代码break在代码中的给定行设置一个断点,即当代码运行时它将停在该行step步骤一行step n步骤 n 行finish运行程序直到结束
请注意,这是一个非常简单的介绍,以表明您想要做什么是可能的。在现实生活中,您必须更多地了解您的调试器,它们是非常强大和有用的软件,具有许多甚至这里没有暗示的功能。对于 gdb,您可以通过搜索找到许多教程,例如https://sourceware.org/gdb/onlinedocs/gdb/index.html
ian@eris:~/work/stack$ gfortran -fcheck=all -Wall -Wextra -std=f2008 -g step.f90 -o step
ian@eris:~/work/stack$ gdb step
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from step...done.
(gdb) list
1 Program single_step
2
3 Implicit None
4
5 Integer :: i
6
7 Do i = 1, 10
8 Write( *, * ) 'I is now ', i
9 End Do
10
(gdb) run
Starting program: /home/ian/work/stack/step
I is now 1
I is now 2
I is now 3
I is now 4
I is now 5
I is now 6
I is now 7
I is now 8
I is now 9
I is now 10
[Inferior 1 (process 23965) exited normally]
(gdb) break 1
Breakpoint 1 at 0x5555555548c6: file step.f90, line 1.
(gdb) run
Starting program: /home/ian/work/stack/step
Breakpoint 1, single_step () at step.f90:1
1 Program single_step
(gdb) step
7 Do i = 1, 10
(gdb) step
8 Write( *, * ) 'I is now ', i
(gdb) step
I is now 1
7 Do i = 1, 10
(gdb) step
8 Write( *, * ) 'I is now ', i
(gdb) step
I is now 2
7 Do i = 1, 10
(gdb) step
8 Write( *, * ) 'I is now ', i
(gdb) step 5
I is now 3
I is now 4
I is now 5
7 Do i = 1, 10
(gdb) step
8 Write( *, * ) 'I is now ', i
(gdb) step 2
I is now 6
8 Write( *, * ) 'I is now ', i
(gdb) finish
Run till exit from #0 single_step () at step.f90:8
I is now 7
I is now 8
I is now 9
I is now 10
0x0000555555554a30 in main (argc=1, argv=0x7fffffffe2fa) at step.f90:11
11 End Program single_step
(gdb) quit
A debugging session is active.
Inferior 1 [process 23969] will be killed.
Quit anyway? (y or n) y
ian@eris:~/work/stack$