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.
1) With Ubuntu running on your virtual machine, open the Terminal window.
2) Check to see if you have gfortran installed.
- To check whether you have gfortran already installed type in the command:
- If nothing is returned then gfortran is not installed.
3) Install gFortran
- To install gfortran simply enter the command:
- sudo apt-get install gfortran
3) The gfortran package will then begin the brief installation process and will require you to enter yes or no to continue.
4) Enter Y to finalize the installation process and successfully install gfortran.
5) Once complete, the command ‘which gfortran’ will successfully return a message on the following line showing the directory where gfortran can be found.
6) To successfully run our first “helloworld” script using gfortran, let’s start by creating a file using the unix text editor pico.
- Pico should already be installed on Ubuntu; to perform a quick check, enter the command “which pico”
7) Let’s create our first program by entering the command “pico fortran.f”
- 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.
8) To create a simple helloword script simply enter the code:
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’
8) After exiting you will find yourself back at the terminal window.
9) Let’s try running the script you just created by first compiling the file.
- Enter the command to first compile your script.
- If the terminal does not give you an error then your file has compiled successfully.
10) Run the file by entering the command ./a.out
- This is what you should see:
11) Congratulations! You ran your first gfortran script