VS code 设置Fortran环境

本文档详细介绍了如何在Windows和Linux环境下配置Visual Studio Code(VSCode)进行Fortran编程,包括安装MinGW-w64或gfortran编译器,安装ModernFortran插件和fortls,以及设置tasks.json和launch.json文件,实现代码编译、调试功能。同时提供了Windows和Linux下的具体配置示例。
摘要由CSDN通过智能技术生成

首先要安装编译器,在Windows下安装MinGW-w64,然后在系统环境里添加编译器的路径。在Linux里直接安装gfortran 编译器即可,这个过程不再赘述。

然后需要安装python和vs code。

然后再配置vscode:

Fortran在vscode 里的设置

只要安装一个插件,就是Moden Fortran。其他三个可能组合在一起可以使用(fortran, FORTRAN IntelliSense,Fortran Breakpoint Support),但是与Modern Fortran是冲突的。

必须配合fortls作为后台debug server,用pip install fortls 安装就行。

可以配合fident 或者 fprettify 来格式化代码(pip install)

然后是launch和tasks配置文件:

Windows下的配置文件

tasks.json

{

    "tasks": [

      {

        "type": "shell",

        "label": "gfortran.exe fang",

        "command": "C:\\mingw-w64\\bin\\gfortran.exe",

        "args": [

          "-g",

          "${file}",

          "-o",

          "${fileDirname}\\${fileBasenameNoExtension}.exe"

        ],

        "options": {

          "cwd": "C:\\mingw-w64\\bin"

        }

      }

    ],

    "version": "2.0.0"

  }

luanch.json

{

    // Use IntelliSense to learn about possible attributes.

    // Hover to view descriptions of existing attributes.

    // For more information, visit: Debugging in Visual Studio Code

    "version": "0.2.0",

    "configurations": [

        {

            "name": "gfortran.exe build and debug active file",

            "type": "cppdbg",

            "request": "launch",

            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",

            "args": [],

            "stopAtEntry": false,

            "cwd": "${workspaceFolder}",

            "environment": [],

            "externalConsole": false,

            "MIMode": "gdb",

            "miDebuggerPath": "C:\\mingw-w64\\bin\\gdb.exe",

            "setupCommands": [

                {

                    "description": "Enable pretty-printing for gdb",

                    "text": "-enable-pretty-printing",

                    "ignoreFailures": true

                }

            ],

            "preLaunchTask": "gfortran.exe fang"

        }

    ]

}

Linux下修改一下编译器和调试器的路径即可,比如//改成\,比如C:\\mingw-w64\\bin\\gdb.exe 改成 gdb

要在 VS Code 中搭建 Fortran 环境,您需要安装以下组件: 1. Visual Studio Code 编辑器 2. Fortran 编译器(比如 GNU Fortran 或 Intel Fortran) 3. Fortran 插件(Visual Studio Code 的扩展) 以下是详细步骤: 1. 安装 Visual Studio Code 编辑器 如果您已经安装了 Visual Studio Code,请跳过此步骤。否则,您可以从 Visual Studio Code 官方网站(https://code.visualstudio.com/)下载并安装 Visual Studio Code。 2. 安装 Fortran 编译器 您可以使用 GNU Fortran 或 Intel Fortran。如果您已经安装了 Fortran 编译器,请跳过此步骤。 - GNU Fortran 您可以从 GNU Fortran 官方网站(https://gcc.gnu.org/wiki/GFortran)下载 GCC 编译器套件。安装后,您就拥有了 GNU Fortran 编译器。 - Intel Fortran 您可以从 Intel Fortran 官方网站(https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/fortran-compiler.html)下载 Intel Fortran 编译器。安装后,您就拥有了 Intel Fortran 编译器。 3. 安装 Fortran 插件 打开 Visual Studio Code 编辑器,点击左侧菜单栏上的“扩展”图标,搜索“fortran”,选择“Fortran”插件并安装。安装完成后,您就可以使用 Visual Studio Code 进行 Fortran 开发了。 4. 配置编译器路径 打开 Visual Studio Code 编辑器,按下“Ctrl+Shift+P”打开命令面板,在面板中输入“settings.json”,选择“Open Settings (JSON)”选项。在打开的 JSON 文件中,添加以下内容: ```json "fortran.compilerPath": "/path/to/fortran/compiler" ``` 将`/path/to/fortran/compiler`替换为您的 Fortran 编译器路径。保存并关闭文件。 现在,您就可以在 Visual Studio Code 中创建 Fortran 项目,并使用您的 Fortran 编译器进行编译和运行了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值