尝试在VSCode上运行Fortran代码。这是我第一次使用VSCode,所以我不太熟悉。
如何编译文件? (根据我在网上看到的内容,该终端使用的终端带有“ gfortran 文件 .f95”。但是,这对我不起作用。
有没有一种方法可以在VSCode中进行构建和编译?
知道这是一种古老的语言,在线教程已经过时了。
谢谢!
答案 0 :(得分:1)
是的,可以在 Fortran 环境中使用 VS Code。我使用 Microsoft 开发的“CMake Tools”扩展来构建程序。如果您是 Windows 用户,“最简单”的方法是在 Windows 10 上安装 WSL (Ubuntu 20.xx)。然后您就可以直接从 VS Code 获得所有 Linux 工具,例如 gfortran 和 gdb。在 Ubuntu 中以“通常”的方式安装这些工具:
sudo apt install build-essential sudo apt 安装 gfortran 等等。 等
program hello
! This is a comment line, it is ignored by the compiler
print *, 'Hello, World!'
end program hello
# CMake project file for hello.f
cmake_minimum_required (VERSION 3.6)
project (hello_world)
enable_language (Fortran)
# make sure that the default is a RELEASE build
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE RELEASE CACHE STRING
"Choose the type of build, options are: None Debug Release."
FORCE)
endif (NOT CMAKE_BUILD_TYPE)
# default installation
get_filename_component (default_prefix "hello_world" ABSOLUTE)
set (CMAKE_INSTALL_PREFIX ${default_prefix} CACHE STRING
"Choose the installation directory; by default it installs in the /usr directory."
FORCE)
# FFLAGS depend on the compiler
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)
if (Fortran_COMPILER_NAME MATCHES "gfortran.*")
# gfortran
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -std=legacy")
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g -std=legacy")
elseif (Fortran_COMPILER_NAME MATCHES "ifort.*")
# ifort (untested)
set (CMAKE_Fortran_FLAGS_RELEASE "-f77rtl -O3")
set (CMAKE_Fortran_FLAGS_DEBUG "-f77rtl -O0 -g")
elseif (Fortran_COMPILER_NAME MATCHES "g77")
# g77
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -m32")
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g -m32")
else (Fortran_COMPILER_NAME MATCHES "gfortran.*")
message ("CMAKE_Fortran_COMPILER full path: " ${CMAKE_Fortran_COMPILER})
message ("Fortran compiler: " ${Fortran_COMPILER_NAME})
message ("No optimized Fortran compiler flags are known, we just try -O2...")
set (CMAKE_Fortran_FLAGS_RELEASE "-O2")
set (CMAKE_Fortran_FLAGS_DEBUG "-O0 -g")
endif (Fortran_COMPILER_NAME MATCHES "gfortran.*")
# build executables
set (EXECUTABLES "hello_world")
add_executable ("hello_world" "hello.f")
# install executables
install (TARGETS ${EXECUTABLES}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
{
// 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) hello",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/hello_world",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
}
]
}
现在您已准备好配置、构建和调试您的程序。
答案 1 :(得分:0)
在系统上安装Fortran编译器。
gfortran
软件包基于debian的系统(例如ubuntu)的Linux示例:
sudo apt-get update
sudo apt-get install gfortran
然后,在安装后,您可以编译在VSCode中编辑的fortran文件。
在终端中,您需要使用
g95 –c hello.f90
将hello.f90编译为名为hello.o的目标文件
g95 hello.f90
编译hello.f90并将其链接以生成可执行文件a.out`
对于Linux
gfortran hello.f90
答案 2 :(得分:0)
您没有告诉我们您在VS中使用什么Fortran编译器,您确实需要提供有关在VS环境中尝试过的内容的更多信息,以便我们帮助您取得进展。我怀疑您可能尚未在系统上安装Fortran编译器。因此,除了提供替代的整洁解决方案外,我无能为力,我今天早晨从印度工程科学技术学院的Kalyan Kumar Bhar教授那里收到了邮件(在fortran-lang@groups.io
邮件列表中):
如果要在Windows上编译Fortran代码,可以采用多种方法。一世 下面提到使用GNU编译器的最简单的过程之一:
此解决方案已由邮件列表中的一位用户立即进行了测试,并且效果很好。我还没有亲自尝试过,但是它似乎是将Intel Parallel Studio与Windows上的Visual Studio结合使用的绝佳可行的现代Fortran替代方案。