Lệnh run fortran trên linux

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!

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:

  • gfortran is the name of the Fortran compiler. This is a "free" compiler that often is included in Linux distributions. Your compiler may have a different name such as ifort or pgf77.
  • The -o option tells the compiler that the next word (here, myprogram) will be the name of the binary version of the program. If you omit this option, most systems will use the default name a.out for the binary version regardless of the name of your program. It's OK to use this default, though it's usually helpful to use a more meaningful name. The binary file is often called the executable file because the computer can run (execute) it.
  • myprogram.f is the name of the file that contains the source code of your program. The source code is the file of instructions that you actually edit and type. Your source file could be named something other than myprogram.f, such as homework1.f. or some other descriptive name. On many systems the name of the source file must end with the suffix .f or the compiler will become unhappy.

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 .exe program is not a FORTRAN program, it's a Windows executable, and nor Bash nor a FORTRAN compiler will understand it.

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:

$ wine yourprogram.exe

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 :)