Shawn Lim
Shawn Lim

Reputation: 11

Having trouble running Fortran on Visual Studio Code

Trying to run Fortran code on VSCode. It is my first time using VSCode, so I am not too familiar.

How do I compile the file? (From what I saw online, it uses the terminal with: "gfortran file.f95". However, this does not work for me.

Is there a way to have a button to build and compile within VSCode?

Knowing that this is an old language the tutorials online are quite outdated.

Thanks in advance!

Upvotes: 1

Views: 18805

Answers (2)

transmitterdan
transmitterdan

Reputation: 21

Yes, it is possible to use VS Code in a Fortran environment. I use the Microsoft developed "CMake Tools" extension for building the program. The "easiest" way, if you are a Windows user, is to install WSL (Ubuntu 20.xx) on Windows 10. Then you have all of the Linux tools such as gfortran and gdb available directly from VS Code. Install these tools the "usual" way in Ubuntu:

sudo apt install build-essential sudo apt install gfortran etc. etc.

  1. Install VS Code for Windows.
  2. Install the VS Code "C/C++ Extension Pack" by Microsoft and enable the insiders edition. That lets you create a project in WSL from native Windows VS Code IDE.
  3. Install the "CMake Tools" extension from Microsoft.
  4. Connect VS Code to WSL and create a folder for your project.
  5. Create hello.f (there are lots of examples on the internet). Here is one example:
program hello
  ! This is a comment line, it is ignored by the compiler
  print *, 'Hello, World!'
end program hello
  1. You need to create a CMakeLists.txt file for your project. Put in same folder as hello.f. Here is a simple one for "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. You also need to create a file called .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",
        }
        
    ]
}

Now you are ready to configure, build and debug your program.

Upvotes: 2

tHeSiD
tHeSiD

Reputation: 5333

Install a Fortran compiler on your system.

  • For Windows download and install from here : G95 - Windows Download (use recommended option to automatically set the path in windows)
  • For linux use the package manager to install gfortran package

Linux Example for debian based systems like ubuntu:

sudo apt-get update
sudo apt-get install gfortran

Then after installation you can compile your fortran files you edit in VSCode.

In the Terminal you need to use

g95 –c hello.f90

Compiles hello.f90 to an object file named hello.o

g95 hello.f90

Compiles hello.f90 and links it to produce an executable a.out`

For Linux

gfortran hello.f90

Upvotes: 0

Related Questions