• Aucun résultat trouvé

Compiling Fortran

Dans le document For Mary (Page 162-171)

137

F

ortran is renowned for its ability to handle intricate mathematical computations.

This has caused it to remain an important language in the scientific community.

In some scientific circles, such as physics, Fortran is the predominant language.

The GNU Fortran compiler is primarily based on the ANSI standard definition of Fortran 77, but it is by no means limited to that. It contains many (but not all) features and characteristics defined in the Fortran 90 and Fortran 95 standards documents. The Fortran language is as much a tradition as it is a standard, and the standards documents themselves leave a lot in the hands of the implementers of the compilers. Every Fortran compiler works primarily the same way, but each supports its own dialect of the language.

Fundamental Compiling

Table 7-1 lists the file name suffixes that are involved with compiling and linking Fortran programs. A table listing all the suffixes recognized by GCC can be found in Appendix D.

Single Source to Executable

A traditional Fortran program is written using all uppercase, and the first six character positions of each line are reserved for special purposes. The first column is reserved for the characterCto indicate that the entire line is a comment. The second through sixth columns are reserved for labels. The code begins in the seventh column. The following example is a program formatted in the traditional Fortran format:

C helloworld.f C

PROGRAM HELLOWORLD WRITE(*,10)

10 FORMAT('hello, world') END PROGRAM HELLOWORLD

The GCC compiler does not require that the source be all uppercase, but, unless specified otherwise, the fixed format is required. The following command will compile the program into an executable:

$ g77 helloworld.f -o helloworld

Theg77command is a front end forgccand sets up the basic environment requirements of a Fortran program. The same result can be achieved by using agcc command as follows:

$ gcc helloworld.f -lfrtbegin -lg2c -lm -shared-libgcc -o helloworld

USINGTHECOMPILERCOLLECTION

The librarylibfrtbegin.a(invoked by the command line option-lfrtbegin) contains the startup and exit code necessary to start a Fortran program running and to terminate it cleanly. The librarylibg2c.acontains the necessary Fortran runtime routines for such fundamental capabilities as input and output. The librarylibm.ais the system math library. The-shared-libgccoption specifies that the shared version of the standard librarylibgccbe used.

GCC also allows Fortran code to be compiled in a free form format. Comments are formed beginning with an exclamation point (!) character and continuing to the end of the line. A free-form version of the previous program can have the statements, and labels, begin in any column, as follows:

! helloworldff.f

!

Program Helloworld write(*,10)

10 format('hello, world') end Program Helloworld

This program can be compiled the same as the previous one by adding the -ffree-formoption to the command line, as follows:

$ g77 -ffree-form helloworldff.f -o helloworldff

Because of some of the fundamental differences between the two syntactic forms, programs are written in either free form or fixed form format—it is difficult to write a program that will compile in either form because of differences in the syntax of the comments and the general layout rules.

Suffix File Contains

.a Static object library (archive)

.f, .for, .FOR Fortran source code that is not to be preprocessed .F, .fpp, .FPP Fortran source code that is to be preprocessed

.o An object file in a format appropriate to be fed to the linker .r Fortran source code to be preprocessed by Ratfor

.so Shared object library

Table 7-1. File Name Suffixes in For tran Programming

Multiple Source Files to Executable

Theg77command is capable of compiling and linking multiple Fortran source files into a single executable. The following listing is the mainline of a simple program, stored in a file namedcaller.f, that makes a single function call and displays the result:

C caller.f C

PROGRAM CALLER

I = Iaverageof(10,20,83) WRITE(*,10) 'Average=', I 10 FORMAT(A,I5)

END PROGRAM CALLER

The function namedIaverageis defined in a separate source file namedcalled.f, as follows:

C called.f C

INTEGER FUNCTION Iaverageof(i,j,k) Iaverageof = (i + j + k) / 3 RETURN

END FUNCTION Iaverageof

These two source files can be compiled and linked into an executable named caller with the following statement:

$ g77 caller.f called.f -o caller

The same result can be achieved in three separate steps by first creating an object file for each of the source files and then linking the object files into an executable, as follows:

$ g77 -c caller.f -o caller.o

$ g77 -c called.f -o called.o

$ g77 caller.o called.o -o caller

Generating Assembly Language

The-Soption instructsg77to generate assembly language from the source code and then stop. To generate assembly language of thehelloworld.fexample used earlier in this chapter, enter the following command:

$ g77 -S helloworld.f

The resulting assembly language file is namedhelloworld.s. The exact form of the assembly language depends on the target platform.

Preprocessing

Compiling a Fortran program with a file suffix of.F,.fpp, or.FPPwill cause the source to be preprocessed before it is compiled. This is the preprocessor, described in Chapter 3, originally designed to work with the C programming language. The following example is a Fortran free form program that uses the preprocessor to include a function into the main program:

The source code of the functionirue()is in the file namediruefunc.h, and it will compile differently depending on whether the macroROUNDUPhas been defined.

The function will round any odd number to an even number. By default, it will round down, but ifROUNDUPhas been defined, the function will round up to get an even number. The body of theirue()function is as follows:

integer function irue(i)

#endif end if

end function irue

The following command line will compile this program into an executable:

$ g77 -ffree-form evenup.F -o evenup

It is not necessary to write a program in free form format to be able to use the preprocessor. Because the preprocessor discards the directives and passes only the resulting code to the compiler, the following program is also valid:

C adder.F C

#define SEVEN 7

#define NINE 9 C

program adder isum = SEVEN + NINE write(*,10) isum 10 format(I5)

end program adder

Creating a Static Library

A library of object modules can be created by compiling Fortran source code into .o files and then using thearutility to store the object files into anarchivefile, which is another name for a static library.

The following example demonstrates the creation of a library containing a pair of simple functions that are both called from the same mainline program. The first function is namedimaximum()and returns the largest of the three integers passed to it:

C imaximum.f C

INTEGER FUNCTION imaximum(i,j,k) iret = i

IF (j .gt. iret) iret = j IF (k .gt. iret) iret = k imaximum = iret

RETURN

END FUNCTION imaximum

USINGTHECOMPILERCOLLECTION The second function is very much like the first, except that it returns the smallest of

the three integers passed to it:

C iminimum.f C

INTEGER FUNCTION iminimum(i,j,k) iret = i

IF (j .lt. iret) iret = j IF (k .lt. iret) iret = k iminimum = iret

RETURN

END FUNCTION iminimum

The following three commands compile these two functions and store them in the library:

$ g77 -c iminimum.f -o iminimum.o

$ g77 -c imaximum.f -o imaximum.o

$ ar -r libmima.a imaximum.o iminimum.o

The-coption ong77instructs the compiler to compile the source into an object file but not to invoke the linker. Thearutility with an-roption will create a library named libmima.aif it does not already exist. If the library does exist, any object files inside it will be replaced by the ones named on the command line.

The following program calls the two functions stored in the library and displays the result:

C minmax.f C

PROGRAM MINMAX

WRITE(*,10) 'Maximum=', imaximum(10,20,30) WRITE(*,10) 'Minimum=', iminimum(10,20,30) 10 FORMAT(A,I5)

END PROGRAM MINMAX

This program can be compiled and linked to the functions stored in the library with the following command:

$ g77 minmax.f libmima.a -o minmax

The compiler recognizesminmax.fas Fortran source, so it compiles the source into an object file and then links the program into an executable by passing the name of the librarylibmima.ato the linker.

Creating a Shared Library

The creation of a shared library is much the same as the creation of a static library, but object files to be stored in the library must be compiled with either the-fpicor-fPIC option so that the code can be loaded into memory and executed while the program is running. (PIC stands forposition independent code.)

Using the same source code as used in the static library example, the two object files and the shared library can be created with the following commands:

$ g77 -c -fpic iminimum.f -o iminimum.o

$ g77 -c -fpic imaximum.f -o imaximum.o

$ g77 -shared iminimum.o imaximum.o -o libmima.so

The-coption is necessary to instruct the compiler to produce.oobject files, and -fpicis required to have the object files produced in the correct format for being loaded from a shared library at runtime. The-sharedoption combines all the object files on the command line into a shared library namedlibmima.so. For the library to be used by an application, it is necessary for the program to locate the library when it starts running, as described in Chapter 12.

To compile and link the program to use the shared library, it is only a matter of including the name of the shared library on the command line as the program is linked:

$ g77 minmax.f -lmima -o minmax

The-loption specifies the library name as mima, which the compiler expands to libmima.so and searches for a library by that name in the places the system is configured to look for all shared libraries.

Ratfor

Ratfor is an acronym for Rational Fortran. It is a publicly available preprocessor of source code that allows Fortran to be written with C-like syntax and then be converted into standard Fortran to be compiled.

The original Ratfor translator was implemented by Kernighan and Plauger in 1976. Since its inception at AT&T, there have been a number of versions of Ratfor.

The two latest ones can be freely downloaded from a number of locations, including http://sepwww.stanford.edu/software/ratfor.html for ratfor77 and http://sepwww.

standord.edu/software/ratfor90.html for ratfor90. The downloads are very small, and installation is quite simple. The installation procedure that comes with them will install the executable as eitherratfor77(which is a C program that compiles into a binary executable) orratfor90(a Perl script). Either of these can be used to generate Fortran code for input into the GCC compiler.

USINGTHECOMPILERCOLLECTION The two versions of Ratfor are different enough that you will need to select one and stay with it. Ratfor90 is not a direct extension of Ratfor77. It is very easy to write simple programs that will compile with one but will not compile with the other.

The source code of a Ratfor program is Fortran and can be written as pure Fortran, but there are many C constructs available. The following simple example demonstrates the form and appearance of a Ratfor program:

# ratdemo.r

This code can be processed throughratfor77and compiled into an executable with the following two commands:

$ ratfor77 <ratdemo.r >ratdemo.f

$ g77 ratdemo.f -o ratdemo

The fileratdemo.f, output from the Ratfor translator, is Fortran and looks like the following:

C Output from Public domain Ratfor, version 1.0 program ratdemo

Dans le document For Mary (Page 162-171)