Friday, September 3, 2010

Hello!

A Brief Introduction

First, please note that I am using Mac OS X 10.6 for these examples. The command line syntax is not very different from Linux operating systems, but does vary slightly from Windows systems. If I achieve my goal with this blog, the differences will be explained at a later time.

Also, when sections appear with a "$" character as the left-most character, it means that those commands are supposed to be entered by you on the command line (Terminal on Mac/Ubuntu), even though the command line for Mac by default prompts with something like:

user-computer:folder user$

After running an executable, any command line output that is successful will be denoted in green.

To the Fortran...

This is a simple Fortran program that will print out "Hello!" to stdout. The below code must be written in a file with a Fortran extension (use .f95). Using an editor such as Text/Edit, create a new file named:

HelloProgram.f95

and enter the following source code:

program hello
  implicit none
  print *, "Hello!"
end program hello

Close the editor to save the file to disk. In order to get the program to run, the source code must be compiled and linked using a Fortran compiler. Luckily there are a few free Fortran compilers available. Currently I am using gfortran (GNU Fortran): http://gcc.gnu.org/fortran/

More on how to install these compilers later. From the command line, compile, link, and run the program using the following commands:

$ gfortran HelloProgram.f95 -c
$ gfortran HelloProgram.o -o hello
$ ./hello
Hello!

The above can be shortened to one line (the "-o" tells gfortran to compile and link the source code):

$ gfortran HelloProgram.f95 -o hello
$ ./hello
Hello!

No comments:

Post a Comment