Fortran Linux Compiler Steps New


  • This page is about how to compile and run Fortran programs under unix.
  • For a basic introduction, skip the shaded boxes, and stop after the section called “Compiling a single source file”
  • Introduction

    Before start learning the Fortran programming language, you will need to ensure that you can compile your programs on a unix system.

    For this purpose, there are some example programs in the directory /d/graemsay/foundation/examples/fortran on the astrophysics system. This document will tell you how to compile and run them. All they do is print out the string “Hello, world“, but it will be enough to prove that you can compile and run a program. (You don’t actually need to understand how they work at this stage.)

    When compiling a program, you will usually want to ensure that the source files are in a directory where you have write permission to create new files. So start by using the cp command to copy the files from directory /d/graemsay/foundation/examples/fortran into a directory of your own.

    Compiling a single source file

    Let’s start by compiling the program helloworld.f. Type:

    gfortran helloworld.f

    if you list files in the current directory, you should find that this has produced a program called a.out. You can then run it by typing:

    ./a.outwhere the “./” specifies a filename in the current directory.

    Now let’s suppose that you want to give your executable program the name “sayhello“, rather than the default (and not very informative) name “a.out“. Of course, you could just rename the file, but it is better to use the “-o” option to tell f77 what filename to create.

    gfortran -o sayhello helloworld.f

    And of course, to run it you would type:

    ./sayhello

    Compiling multiple source files fortran

    So far, you’ve seen how to compile a single source file into an executable program. But with more complicated programs, the whole source code may will be divided over several files.

    Another version of the “Hello, world” program is contained in the files greet.f, hello.f and world.f, which you should have copied from /d/graemsay/foundation/examples/fortran. To make it work, you need to compile together all these files. (This example is admittedly rather contrived  - you wouldn’t normally use this many files just to say “Hello, world” - but it will show you how to compile several files together.)

    Compiling multiple source files in one statement Fortran

    To compile the program, making the executable sayhello, type:

    gfortran -o sayhello greet.f hello.f world.f

    Compiling multiple source files separately

    The above statement should hopefully have produced the program sayhello. But there is one drawback with this method: if you change any one of the files, then you have to recompile them all. For a smallish program, this may not matter very much, but for a large program it is better to use the following method which means you only have to recompile the file which you actually changed.

    In fact in the above examples, f77 does two things:

    • compiling
    • linking

    With the “-c” flag, you can make it stop after the compile stage. Type:

    gfortran -c greet.f
    gfortran -c hello.f
    gfortran -c world.f

    List files, and you’ll see that this has produced “object files”, greet.o, hello.o, world.o. Object files have undergone compilation, but have not yet been built into an executable program which you can run.

    To link them together, making the executable sayhello,  type:

    gfortran -o sayhello greet.o hello.o world.oWith this command, the f77 only does the linking stage. (That is: it doesn’t omit the linking stage, because you haven’t specified “-c“; and it doesn’t do any compilation, because all the input files are object (.o) files which have already been compiled.)

    If  you type:

    ./sayhelloit should tell you:

    Hello, world

    Now suppose that you want your program to print “Hello, world.” (with a full stop). Edit the file greet.f, changing the line:

    100 format(A5,', ',A5)into

    100 format(A5,', ',A5,'.')To recompile the program, type:

    gfortran -c greet.f
    gfortran -o sayhello greet.o hello.o world.o

    This should have updated the sayhello program to include your change. And you’ll see that you didn’t have to recompile the bits of the program which you didn’t change (hello.f, world.f), you just used the object files (hello.o, world.o) from before.

    Making and using libraries

    If you have a set of subroutines which you will use in many programs, it may be useful to make the object files into a library.

    As an example, suppose that you want to make a library, consisting of the subroutines contained in the files hello.f and world.f.

    First you would compile the source files to make object files:

    gfortran -c hello.f
    gfortran -c world.f

    Then would you use the “ar r” command to insert the object files into the library (creating a new library if it doesn’t already exist). The library should be called libsomething.a, so let’s choose “libgreetings.a“. You could type:

    ar r libgreetings.a hello.o
    ar r libgreetings.a world.o or simply:

    ar r libgreetings.a hello.o world.o

    You can list the contents of the archive with the “ar t” command.

    ar t libgreetings.aNow let’s make use of the library by compiling greet.f. You would type:

    gfortran -o sayhello greet.f -L. -lgreetingsHere, “-L” speficies where to find libraries, so here we specify “.” (the current directory), and “-lsomething” means to use the library libsomething.a. The linker finds the routines hello and world in the library (to prove the point, try deleting the “.o” files from the directory and compiling again).

    Notes:

    • If you add to a library an object file which already exists, the “ar r” command will Replace the old version.
    • You can Delete object files from a library with “ar d“, e.g. “ar d libgreetings.a hello.o
    • You can speed up linking to your library by making an up-to-date index (or “Symbol table”). Type: “ar s libgreetings.a

    As well as libraries which you make yourself, you may sometimes need to link to standard libraries. For example, if your fortran program contains NAG library subroutines, you would use “-lnag” to link to the NAG libary. Hopefully in this case the library will be installed in a standard location (e.g. /usr/lib), so you shouldn’t need to use the “-L” flag.

    Related Posts:

    • No Related Posts
    Related Websites
    Posted in : Support
    Tags: ,

    Leave a Reply