How to Run a Hello World script using GFortran
What is GFortran?
GNU Fortran is a part of GCC, the GNU Compiler Collection. GCC consists of a collection of front ends for various languages, which translate the source code into a language-independent form called GENERIC. This is then processed by a common middle end which provides optimization, and then passed to one of a collection of back ends which generate code for different computer architectures and operating systems. Additional information regarding what GFortran is can be found here.
This short tutorial is a guide to help familiarize you with writing a simple Hello World program using the GFortran compiler using Ubuntu.
Follow the Ubuntu tutorial.
To check whether you have gfortran already installed type in the command:
which gfortran
If nothing is returned then gfortran is not installed.
To install gfortran simply enter the command:
sudo apt-get install gfortran
Pico should already be installed on Ubuntu; to perform a quick check, enter the command “which pico”
The terminal window will then open up a blank file window with several file commands located at the bottom.
This window is where you will create your first fortran file that you will compile using the gfortran compiler.
Note: It is important to include the ‘.f’ after every file that you wish to compile using the gfortran compiler.
program helloworld
print *, “Hello World”
end program helloworld
Once complete you will want to “Write Out” your file by holding CTRL and pressing ‘O’
Writing Out is equivalent to saving your file.
After pressing CTRL+O you will be asked what you would like to name the file.
Leave the name as is fortran.f and hit Enter.
You have officially created a helloworld file!
Exit the file by holding CTRL and pressing ‘X’
Enter the command to first compile your script.
gfortran fortran.f
If the terminal does not give you an error then your file has compiled successfully.
This is what you should see: