文章目录
Visual Studio Code:Fortran
编译Fortran
终端运行代码
1 gfortran hello.f90
2 ./a
1 将代码编译为可执行文件,不特别设置一般为a.exe
2 运行a.exe
终端运行多个代码
1 gfortran -o hello hello.f90 hello1.f90
2 ./hello
1 将hello.f90文件和hello1.f90文件编译为名为hello.exe的可执行文件,也可只编译一个文件
2 运行hello.exe
Debug运行代码
- 在需要编辑的文件所在文件夹内新建名为.vs code的文件
- 在.vs code文件夹下新建launch.json文件和tasks.json文件
- 此时可以在需要编译的代码文件界面通过 Ctrl + Shift + B 来编译文件,并生成同名可执行文件。也可以直接点击运行–> 启动调试 来运行代码
PS:
launch.json文件和tasks.json文件内容如下,参数意义可参考视频:VS Code for Fortran Ep.3: Automated Build Process
launch.json文件
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Fortran",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [], // Possible input args for a.out
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Fortran: gfortran.exe 生成活动文件"
}
]
}
tasks.json文件
{
"version": "2.0.0",
"tasks": [
{
//"type": "cppbuild",
"label": "Fortran: gfortran.exe 生成活动文件",
"command": "gfortran.exe",
"args": [
"-g",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: gfortran.exe"
}
]
}
Fortran相关扩展
- C/C++
- fortran
- Fortran Breakpoint Support
- FORTRAN IntelliSense
- Modern Fortran
Visual Studio Code基本操作
切换为中文
- 在扩展库搜索 Language Packs
- 下载简体中文包
- 点击右下角弹出的Restart重启即可
Visual Studio Code显示标尺
- 文件 --> 首选项 --> 设置
- 搜索 Rulers --> 在settings.json中编辑
- 增加以下代码,Ctrl+S保存并返回即可
"editor.rulers": [132],
"workbench.colorCustomizations": {
"editorRuler.foreground": "#458cff"
- 当一行代码的长度超过最大标尺时,可分成两行,并在第一行末尾加上&
参考链接:VS Code for Fortran Ep.2: Extensions & Rulers
重启扩展
- Ctrl + Shift + P
- 输入 reload Window