« previous next»

1.4    Compiling in UNIX

    Your code must be compiled before you can run it. The compiler is a program that translates your source code into machine language, or an object program.  You must first learn the name of the compiler installed on your machine and the command that invokes it. For the example below, we will assume that the compiler can be invoked by the command gfortran.

In order to compile a program named progname.f90, issue the following command at the prompt:

$ gfortran progname.f90

This will create an executable file named a.out. You should then be able to run the program by typing the following command:

$ ./a.out

Every time you compile a program without  the -o option (discussed in the next paragraph) a new a.out file will be created. If a previous a.out file exists, it will be overwritten.

    Many options are available when compiling code and some of these will be discussed later in the notes.  One option (-o) allows you to define the name for your executable file.  You do not have to use the default executable named a.out.  In this way you can compile a program once and have the executable available whenever it is needed.  Also, in more advanced cases, two or more executables will need to be linked.  In these cases, you must have different names for all executables.  For example:

$ gfortran -o progname progname.f90

    This command compiles the program progname.f90 and creates an executable file named progname.  The typical convention is to name your executable file with the same name as your program, but without an extension.

Practice: Compile and run the program written previously in 1.3.
 

« previous next»