Using Fortran and WSL in VS Code

1、安装基础

  • sudo apt-get update
  • sudo apt-get install build-essential gdb
  • sudo apt-get install gfortran

查看位置

  • whereis g++
  • whereis gdb
  • whereis gfortran

2、安装插件

(1)WSL
(2) C/C++
(3) Modern Fortran (or fortran)
(4) Fortran Breakpoint Support

3、配置文件

(1) launch.json

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "gfortran build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ],
        "preLaunchTask": "gfortran build active file",
        "miDebuggerPath": "/usr/bin/gdb"
      }
    ]
  }

(2) tasks.json

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "gfortran build active file",
        "command": "/usr/bin/gfortran",
        "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe"],
        "options": {
          "cwd": "/usr/bin"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

4、出错处理

(1) 如果单步debug报类似错误:

Unable to write file ‘vscode-remote://wsl+ubuntu-18.04/build/glibc-OTsEL5/glibc-2.27/csu/libc-start.c’ (NoPermissions (FileSystemError): Error: ACCESS: permission denied, open…

解决:

$sudo mkdir -p /build/glibc-OTsEL5
$cd /build/glibc-OTsEL5
$sudo wget http://ftp.gnu.org/gnu/glibc/glibc-2.27.tar.gz
$sudo tar –zxvf glibc-2.27.tar.gz

(2) 如果报错:

#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (/build/glibc-OTsEL5/glibc-2.27/csu/libc-start.c).

解决:
新增c_cpp_properties.json文件,内容如下,其中”/usr/include”是手动添加的。

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

Leave a Reply

Your email address will not be published. Required fields are marked *