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? Show 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 ProgramCreate 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 systemMake 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 programCompile 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! Before your program can be executed, it must be compiled. This converts the English-like text of a Fortran program into a binary form that the processor understands. You compile a program by typing a line similar to the following: gfortran -o myprogram myprogram.f It is important that you understand what is happening here:
To run the program, simply type the name of the executable file: myprogram On Unix systems (or Linux, or other Unix variants) you may need to explicitly state that the program resides in your current directory. The shorthand for the current directory is . (a period, or "dot"). Then the program would be run as: ./myprogram Next: The simplest Fortran program. Back to the main page for the programming tutorial. It is important to understand the following: Your When I say that it's not a FORTRAN program I really mean it. It might have been a FORTRAN listing on some engineer's computer, but once compiled, it is indistinguishable(*) from a Windows executable built in BASIC, Pascal or C++. http://en.wikipedia.org/wiki/Compiler So the way to go here is to load it in a Windows emulator - Wine, for example:
Of course you can also run it in a Windows virtual machine using VirtualBox or the virtualization environment of your choice. If you can get the FORTRAN sources (and it's not overly complex or dependent on specific libraries) you might want to try to compile it under Linux. (*) This is not entirely correct, but that's the gist of it unless you want to delve into the topics of reverse engineering and decompilation :) |