VsCode Fortran开发环境配置

本文介绍如何在 VSCode 中设置 Fortran 开发环境,包括安装必要的插件、配置编译和调试环境等。提供了 makefile 示例及 Fortran 代码格式化、智能提示等实用技巧。

VsCode Fortran Settings

Modern Fortran [Miguel Carvajal]

Fortran highlight plugin.

C/C++ for Visual Studio Code [Microsoft]

C/C++ debug plugin also supports fortran.

Toggle debug and modify the example launch.json and tasks.json files.

Makefile Tools [Microsoft, optional]

You can set make command in tasks.json or use this plugin to compile make project.

// vscode settings
{
	"makefile.launchConfigurations": [
		{
			"cwd": "/home/user/project",
			"binaryPath": "/home/user/project/bin/program",
			"binaryArgs": []
		}
	]
}

Here is a example for C/C++ make project.

Directory tree

- bin
-- main
- src
-- main.cpp
-- module.cpp
-- header.h
- Makefile

Makefile

LINK    = @echo linking $@ && g++
GCC     = @echo compiling $@ && g++
GC      = @echo compiling $@ && gcc
AR      = @echo generating static library $@ && ar crv
FLAGS   = -g -DDEBUG -W -Wall -fPIC
GCCFLAGS =
DEFINES =
HEADER  = -I./
LIBS    =
LINKFLAGS =

BIN_PATH = bin
SRC = $(wildcard src/*.cpp)
INCLUDES = include
TARGET = main
OBJECT = $(SRC:%.cpp=%.o)

.SUFFIXES: .cpp .c
.cpp.o:
	$(GCC) -c $(HEADER) $(FLAGS) $(GCCFLAGS) -fpermissive -o $@ $<

.c.o:
	$(GC) -c $(HEADER) $(FLAGS) -fpermissive -o $@ $<

$(TARGET) : $(OBJECT)
	@echo "============开始编译============"
	$(LINK) $(FLAGS) $(LINKFLAGS) -o $@ $^ $(LIBS)
	mv $(TARGET) $(BIN_PATH)
	@echo "============编译结束============"

clean:
	rm -rf $(OBJECT) $(TARGET)

FORTRAN IntelliSense (Chris Hansen)

Install fortran-language-server and create .fortls file if you need to modify the default configuration.

pip install fortran-language-server

An example of .fortls to add external source of hdf5 libs.

{
	"ext_source_dirs": ["/home/user/hdf5/fortran/src"],
	"debug_log": true
}

fprettify (Blamsoft)

Fortran code formatter.

pip install --upgrade fprettify

Integrating with VsCode, modify in your need:

// VsCode settings
{
	"fprettify.arguments": "-i 4 --case 1 1 1 2"
}

作者:PorYoung
原文地址:https://blog.poryoung.cn/?p=1190
发布时间:2021年3月22

### 如何在 VSCode配置 Fortran 开发环境 #### 1. 安装 Visual Studio Code 为了开始配置,首先需要下载并安装最新版本的 Visual Studio Code (VSCode)[^3]。 #### 2. 安装必要的扩展插件 要在 VSCode 中支持 Fortran 的开发工作流,需安装以下三个主要扩展: - **Fortran**: 提供基础语法高亮功能。 - **Modern Fortran**: 支持现代 Fortran 特性和 IntelliSense 功能[^2]。 - **Fortran Breakpoint Support**: 增强调试体验中的断点设置能力。 这些扩展可以通过 VSCode 的市场页面或者直接在应用内的 Extensions Marketplace 搜索栏中输入名称来找到和安装。 #### 3. 设置编译器工具链 对于 Mac 用户特别是搭载 M1 芯片的 MacBook Pro 设备来说,推荐使用 GNU Compiler Collection (GCC) 来作为 Fortran 编程的语言处理器。可以利用 Homebrew 工具包管理器来进行 GCC 的安装: ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install gcc ``` 完成上述命令后,应确保 gfortran 可通过终端访问以便后续集成到 VSCode 当前的工作区环境中去。 #### 4. 创建 tasks.json 文件用于定义构建任务 进入项目的根目录下 `.vscode` 文件夹内新建名为 `tasks.json` 的文件,并按照如下模板填写相关内容以实现自动化编译过程的支持: ```json { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "/opt/homebrew/bin/gfortran", // 根据实际路径调整此行 "args": ["-o", "${fileBasenameNoExtension}", "-g", "${file}"], "group": { "kind": "build", "isDefault": true }, "problemMatcher": [] } ] } ``` 这里需要注意的是 `/opt/homebrew/bin/gfortran` 这一部分可能依据个人系统的不同而有所变化,请替换为自己机器上的具体位置[^1]。 #### 5. 配置 launch.json 实现调试模式运行 同样位于 .vscode 下创建另一个叫作 `launch.json` 的文档用来指定启动参数从而允许开发者能够方便快捷地执行以及逐步跟踪代码逻辑走向: ```json { "version": "0.2.0", "configurations": [ { "name": "Run Program", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "lldb" } ] } ``` 以上步骤完成后即可基本满足日常基于 VSCode 平台下的 Fortran 应用程序编写需求了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值