Linux server VSCode configures Fortran debugging environment
When using VS Code to write Fortran programs, I have not been very good at using the debugging function, and I use the print results in various places print
. In order to improve work efficiency, I studied how to configure the Fortran debugging environment, read a lot of blogs for this, and made notes and summaries here.
Note: This blog was originally written for the M1 chip Mac to configure the Fortran debugging environment, but the M1 chip does not support it
gdb
, andlldb
as a debugging tool, the value of the intermediate variable cannot be viewed normally, so for the M1 chip Mac, it is currently impossible to use VSCode to build Fortran debugging normally environment.
1. Fortran environment
The compiler used in this article is Intel
out of the box , and it is also feasible for the out of the compiler.ifort
gcc
gfortran
2. VS Code plugin
Go to the VS Code official website to download and install. After completion, open VS Code, click on the extension on the left, and install the plug-in.
name | author | icon | use | installation location |
---|---|---|---|---|
Remote - SSH | Microsoft | Connect to SSH server remotely | local | |
C/C++ | Microsoft | Modern Fortran plugin dependencies | server | |
Modern Fortran | The Fortran Programming Language | Fortran syntax support, highlighting, debugging, etc. | server |
- If you don't use a server, just install the last two plugins locally;
- Only the plug-ins that must be installed are introduced here. For other auxiliary plug-ins such as
Code Runner
,Makefile Tools
etc., you can explore and use them yourself.
3. SSH server connection
Skip this section if you are not using a server.
- After installing
Remote - SSH
the plug-in, there will be a remote resource manager in the left navigation bar - After opening the remote resource manager, click the "+" sign to add the server
- When adding a server,
SSH
just enter the command normally, for exampleSSH -P 1233 [email protected]
,-P
the port number after it can be left unspecified.
4. Configure the debug file
Open the folder, for example , create a folder TEST_FORTRAN
under this folder , create and create two files in the folder , the configuration of the two files is as follows:.vscode
.vscode
launch.json
tasks.json
4.1 launch.json
In most cases, this document can use the following configuration.
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "cppdbg",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/${fileBasenameNoExtension}",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "build"
}
]
}
type
: Specify the debugging compiler
name
: The configuration name displayed in the running and debugging window on the left
program
: The absolute path of the executable file generated by Fortran
preLaunchTask
: The command executed before debugging and running, the command labelbuild
is configured in the tasks.json file
4.2 tasks.json
It is used to configure the commands executed before debugging, and some parameters of the debugging window. The configuration used here is as follows
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "ifort",
"args": [
"-g",
"${fileDirname}/${fileBasename}",
"-o",
"${workspaceFolder}/${fileBasenameNoExtension}"
]
}
]
}
label
preLaunchTask
: corresponds to the label set in launch.json : the type
type
used here : the command executed when calling the terminal : the parameter passed during executionshell
command
args
command
-g
: enable debugging${fileDirname}/${fileBasename}
: Compile a single for file; when compiling multiple for files, modify it to*.f*
-o
: output executable file${workspaceFolder}/${fileBasenameNoExtension}
: Output the name of the executable file,${fileBasenameNoExtension}
indicating the name of the debugged file without the suffix.program
(Note: The name of the executable file here needs to match the parameters set in the launch.json file )
5. Test
5.1 Single file debugging
- Create a new file under
Test_Fortran
the foldertest01.f90
and write the following content
program name
implicit none
real a,b,c
a = 1.0
b = 2.0
c = 3.0
print *,a,b,c
end program name
- Click Run and Debug in the left navigation bar , select the newly established Debug debugging environment, and add breakpoints on lines 5 and 9, as follows
- Click Debug to debug, click the button in the red box in the figure to debug step by step (or use shortcut keys
F10
), when running to line 7, you can see on the left that the value of the variablea
sumb
has been updated to the sum assigned in the program ,1
and2
the variablec
is still Initial value0
, as shown in the figure below
5.2 Multiple file debugging
- New
test02.f90
, write the subroutine as follows
subroutine test_add(a, b, c)
real, intent(in) :: a,b
real, intent(out) :: c
c = a + b
return
end subroutine test_add
- Modify
test01.f90
, calltest02.f90
,test_add
as follows
program name
implicit none
real a,b,c
a = 1.0
b = 2.0
call test_add(a,b,c)
print *,a,b,c
end program name
- Modify
tasks.json
the file so that it supports compiling multiple files at the same time, the modification is as follows
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "ifort",
"args": [
"-g",
"${fileDirname}/*.f*",
"-o",
"${workspaceFolder}/${fileBasenameNoExtension}"
]
}
]
}
It is used
*.f*
to match files with the suffix.f90
and.for
, and other writing methods can also be used, as long as it can normally match the files that need to be compiled.
- Click the Debug button, the running results are as follows