Linux server VSCode configures Fortran debugging environment

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, and lldbas 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 Intelout of the box , and it is also feasible for the out of the compiler.ifortgccgfortran

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 insert image description here Connect to SSH server remotely local
C/C++ Microsoft insert image description here Modern Fortran plugin dependencies server
Modern Fortran The Fortran Programming Language insert image description here 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 Toolsetc., you can explore and use them yourself.

3. SSH server connection

Skip this section if you are not using a server.

  • After installing Remote - SSHthe 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, SSHjust enter the command normally, for example SSH -P 1233 [email protected], -Pthe port number after it can be left unspecified.

4. Configure the debug file

Open the folder, for example , create a folder TEST_FORTRANunder this folder , create and create two files in the folder , the configuration of the two files is as follows:.vscode.vscodelaunch.jsontasks.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 label buildis 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}"
            ]
        }
    ]
}

labelpreLaunchTask: corresponds to the label set in launch.json : the type
typeused here : the command executed when calling the terminal : the parameter passed during executionshell
command
argscommand

  • -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_Fortranthe folder test01.f90and 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

insert image description here

  • 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 variable asum bhas been updated to the sum assigned in the program , 1and 2the variable cis still Initial value 0, as shown in the figure below

insert image description here

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, call test02.f90, test_addas 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.jsonthe 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 .f90and .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

insert image description here

Guess you like

Origin blog.csdn.net/qq_36529483/article/details/125607807