Question: I would like to understand the basics of how to write and execute a Fortran program on Linux OS. Can you explain it with a simple example?
Answer: In this article, let us review very quickly how to write a basic Hello World Fortran program and execute *.f program on Linux or Unix OS.
1. Write a Hello World Fortran Program
Create helloworld.f program using the Vim editor as shown below.
$ vim helloworld.f program hello print *,"Hello World!" end program hello
You can also write a shorter version of the hello world Fortran program as shown below.
$ vim helloworld.f PRINT *,"Hello World!" END
2. Make sure Fortran compiler is installed on your system
Make sure Fortran compiler is installed on your system as shown below.
$ whereis gfortran gfortran: /usr/bin/gfortran /usr/share/man/man1/gfortran.1.gz $ which gfortran /usr/bin/gfortran $ dpkg -l | grep gfortran ii gfortran 4:4.3.3-1ubuntu1 The GNU Fortran 95 compiler ii gfortran-4.3 4.3.3-5ubuntu4 The GNU Fortran 95 compiler ii libgfortran3 4.3.3-5ubuntu4 Runtime library for GNU Fortran applications
Installing GNU Fortran compiler.
If you don’t have GNU Fortran compiler, install it as shown below.
$ sudo apt-get install gfortran
3. Compile the Fortran program
Compile the helloworld.f using gfortran command as shown below. This will create the a.out file.
$ gfortran -ffree-form helloworld.f $ ls -l -rw------- 1 ramesh ramesh 55 Oct 24 23:13 helloworld.f -rwx------ 1 ramesh ramesh 9545 Oct 24 23:14 a.out*
4. Execute the Fortran program (a.out)
$ ./a.out Hello World! $ mv a.out helloworld $ ./helloworld Hello World!
Get the Linux Sysadmin Course Now!