0%

Using Fortran and WSL in VS Code

  1. 安装基础

    1
    2
    3
    sudo apt-get update
    sudo apt-get install build-essential gdb
    sudo apt-get install gfortran
    查看位置
    1
    2
    3
    whereis g++
    whereis gdb
    whereis gfortran
  1. 安装插件

    (1) WSL
    Remote WSL Plugin
    (2) C/C++
    C/C++ Plugin

    (3) Modern Fortran (or fortran)

    Modern Fortran Plugin

    (4) Fortran Breakpoint Support

    Fortran Breakpoint Support Plugin
  2. 配置文件

    (1) launch.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    {
    "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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    {
    "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
    }
    }
    ]
    }
  3. 出错处理

    (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…

解决:

1
2
3
4
$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”是手动添加的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}