Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
E77790.pdf
Скачиваний:
2
Добавлен:
16.05.2024
Размер:
1.29 Mб
Скачать

2.2 Invoking the Compiler

demo% cat greetings.f

PROGRAM GREETINGS

PRINT *, ’Real programmers write Fortran!’ END

demo% f95 greetings.f demo% a.out

Real programmers write Fortran! demo%

In this example, f95 compiles source file greetings.f and links the executable program onto the file, a.out, by default. To launch the program, the name of the executable file, a.out, is typed at the command prompt.

Traditionally, UNIX compilers write executable output to the default file called a.out. It can be awkward to have each compilation write to the same file. Moreover, if such a file already exists, it will be overwritten by the next run of the compiler. Instead, use the -o compiler option to explicitly specify the name of the executable output file:

demo% f95 -o greetings greetings.f demo% greetings

Real programmers write Fortran! demo%

In the preceding example, the -o option tells the compiler to write the executable code to the file greetings. (By convention, executable files usually are given the same name as the main source file, but without an extension.)

Alternatively, the default a.out file could be renamed via the mv command after each compilation. Either way, run the program by typing the name of the executable file at a shell prompt.

The next sections of this chapter discuss the conventions used by the f95 commands, compiler source line directives, and other issues concerning the use of these compiler. The next chapter describes the command-line syntax and all the options in detail.

2.2Invoking the Compiler

The syntax of a simple compiler command invoked at a shell prompt is:

f95 [options] files...

Here files… is one or more Fortran source file names ending in .f, .F, .f90, .f95, .F90, .F95, or .for; options is one or more of the compiler option flags. (Files with names ending in a

24 Oracle Developer Studio 12.6: Fortran User's Guide • July 2017

2.2 Invoking the Compiler

.f90 or .f95 extension are “free-format” Fortran 95 source files recognized only by the f95 compiler.)

In the example below, f95 is used to compile two source files to produce an executable file named growth with runtime debugging enabled:

demo% f95 -g -o growth growth.f fft.f95

Note - You can invoke the Fortran compiler with either the f95 or f90 command.

New: The compiler will also accept source files with the extension .f03 or .F03. These are treated as equivalent to .f95 and .F95 and could be used as a way to indicate that a source file contains Fortran 2003 extensions.

“2.2.2 Command-Line File Name Conventions” on page 25, describes the various source file extensions accepted by the compiler.

2.2.1Compile-Link Sequence

In the previous example, the compiler automatically generates the loader object files, growth.o and fft.o, and then invokes the system linker to create the executable program file growth.

After compilation, the object files, growth.o and fft.o, will remain. This convention permits easy relinking and recompilation of files.

If the compilation fails, you will receive a message for each error. No .o files are generated for those source files with errors, and no executable program file is written.

2.2.2Command-Line File Name Conventions

The suffix extension attached to file names appearing on the command-line determine how the compiler will process the file. File names with a suffix extension other than one of those listed below, or without an extension, are passed to the linker.

TABLE 1

Filename Suffixes Recognized by the Fortran Compiler

 

 

 

Suffix

Language

Action

 

 

 

.f

Fortran77 or

Compile Fortran source files, put object files in current directory; default name

 

Fortran 95 fixed-

of object file is that of the source but with .o suffix.

 

format

 

 

 

 

Chapter 2 • Using Oracle Developer Studio Fortran

25

2.2 Invoking the Compiler

Suffix

Language

Action

 

 

 

.f95

Fortran 95 free-

Same action as .f

.f90

format

 

 

 

 

.f03

Fortran 2003 free-

Same action as .f

 

format

 

 

 

 

.for

Fortran 77 or

Same action as .f.

 

Fortran 95 fixed-

 

 

format

 

 

 

 

.F

Fortran 77 or

Apply the Fortran (or C) preprocessor to the Fortran 77 source file before

 

Fortran 95 fixed-

compilation.

 

format

 

 

 

 

.F95

Fortran 95 free-

Apply the Fortran (or C) preprocessor to the Fortran 95 free-format source file

.F90

format

before Fortran compiles it.

 

 

 

.F03

Fortran 2003 free-

Same as .F95

 

format

 

 

 

 

.s

Assembler

Assemble source files with the assembler.

 

 

 

.S

Assembler

Apply the C preprocessor to the assembler source file before assembling it.

 

 

 

.il

Inline expansion

Process template files for inline expansion. The compiler will use templates to

 

 

expand inline calls to selected routines. (Template files are special assembler

 

 

files; see the inline(1) man page.)

 

 

 

.o

Object files

Pass object files through to the linker.

 

 

 

.a, .so,. .

Libraries

Pass names of libraries to the linker. .a files are static libraries, .so and .so.n

so.n

 

files are dynamic libraries.

 

 

 

Fortran 95 free-format is described in “4.1 Source Language Features” on page 167.

2.2.3Source Files

The Fortran compiler will accept multiple source files on the command line. A single source file, also called a compilation unit, may contain any number of procedures (main program, subroutine, function, block data, module, and so on). Applications may be configured with one source code procedure per file, or by gathering procedures that work together into single files.

2.2.4Source File Preprocessors

f95 supports two source file preprocessors, fpp and cpp. Either can be invoked by the compiler to expand source code “macros” and symbolic definitions prior to compilation. The compiler will use fpp by default; the -xpp=cpp option changes the default from fpp to cpp. (See also the discussion of the -Dname option).

26 Oracle Developer Studio 12.6: Fortran User's Guide • July 2017

2.2 Invoking the Compiler

fpp is a Fortran-specific source preprocessor. See the fpp(1) man page for details. It is invoked by default on files with a .F, .F90, F95, or .F03 extension.

The source code for fpp is available from Netlib.

See cpp(1) for information on the standard Unix C language preprocessor. Use of fpp over cpp is recommended on Fortran source files.

2.2.5Separate Compiling and Linking

You can compile and link in separate steps. The -c option compiles source files and generates

.o object files, but does not create an executable. Without the -c option the compiler will invoke the linker. By splitting the compile and link steps in this manner, a complete recompilation is not needed just to fix one file, as shown in the following example:

Compile one file and link with others in separate steps:

demo%

f95

-c

file1.f

(Make new object file)

demo%

f95

-o

prgrm file1.o file2.o file3.o

(Make executable file)

Be sure that the link step lists all the object files needed to make the complete program. If any object files are missing from this step, the link will fail with undefined external reference errors (missing routines).

2.2.6Consistent Compiling and Linking

Ensuring a consistent choice of compiling and linking options is critical whenever compilation and linking are done in separate steps. Compiling any part of a program with some options requires linking with the same options. Also, a number of options require that all source files be compiled with that option, including the link step.

The option descriptions in Chapter 3 identify such options.

Example: Compiling sbr.f with -fast, compiling a C routine, and then linking in a separate step:

demo% f95

-c -fast sbr.f

 

demo% cc -c -fast simm.c

 

demo% f95

-fast sbr.o simm.o

link step; passes -fast to the linker

Chapter 2 • Using Oracle Developer Studio Fortran

27

Соседние файлы в предмете Информационные и сетевые технологии