在Visual Studio Code上运行Fortran时遇到问题

时间:2020-06-14 18:12:59

标签: visual-studio-code installation fortran

尝试在VSCode上运行Fortran代码。这是我第一次使用VSCode,所以我不太熟悉。

如何编译文件? (根据我在网上看到的内容,该终端使用的终端带有“ gfortran 文件 .f95”。但是,这对我不起作用。

有没有一种方法可以在VSCode中进行构建和编译?

知道这是一种古老的语言,在线教程已经过时了。

谢谢!

3 个答案:

答案 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 等等。 等

  1. 安装适用于 Windows 的 VS Code。
  2. 安装 Microsoft 的 VS Code“C/C++ 扩展包”并启用 Insiders 版本。这使您可以从本机 Windows VS Code IDE 在 WSL 中创建项目。
  3. 安装来自 Microsoft 的“CMake Tools”扩展。
  4. 将 VS Code 连接到 WSL 并为您的项目创建一个文件夹。
  5. 创建hello.f(网上有很多例子)。下面是一个例子:
program hello
  ! This is a comment line, it is ignored by the compiler
  print *, 'Hello, World!'
end program hello
  1. 您需要为您的项目创建一个 CMakeLists.txt 文件。放在与hello.f相同的文件夹中。这是一个简单的“hello.f”:
# 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})

  1. 您还需要创建一个名为 .vscode/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) hello",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/hello_world",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
        }
        
    ]
}

现在您已准备好配置、构建和调试您的程序。

答案 1 :(得分:0)

在系统上安装Fortran编译器。

  • 对于Windows,请从以下位置下载并安装:G95 - Windows Download(使用推荐的选项可在Windows中自动设置路径)
  • 对于Linux,请使用软件包管理器来安装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编译器的最简单的过程之一:

  1. 转到以下链接: http://www.equation.com/servlet/equation.cmd?fa=fortran
  2. 下载编译器: gcc-10.1.0-32.exe或gcc-10.1.0-64.exe,具体取决于您的操作系统, Windows 10
  3. 安装(最好在C:\ gcc文件夹中)
  4. 您现在可以使用它。但是您可以使用Geany之类的IDE。从下载 https://www.geany.org/(最新版本为1.36)
  5. 安装Geany。最好的部分是它会自动针对以下情况进行配置 GFortran。
  6. 打开Geany。单击文件->新建。然后将其另存为(文件->另存为),例如 Hello.f95
  7. 在此处编写程序。保存(文件->保存)。
  8. 编译,构建和执行(单击Build-> Compile,然后Build-> Build 然后生成->执行)。就是这样。

此解决方案已由邮件列表中的一位用户立即进行了测试,并且效果很好。我还没有亲自尝试过,但是它似乎是将Intel Parallel Studio与Windows上的Visual Studio结合使用的绝佳可行的现代Fortran替代方案。