Наши партнеры








Книги по Linux (с отзывами читателей)

Библиотека сайта rus-linux.net

Linux Compilers and Assemblers

SEP 19, 2003 By Christopher Paul, Rafeeq Rehman. Article is provided courtesy of Prentice Hall.

3.11 Compiling Fortran Programs

The GCC family of compilers also includes g77 which acts as a front end to invoke gcc. The gcc compiler is used to compile Fortran programs. Consider the following simple hello.for Fortran program that write “Hello world” on standard output.

c This program is to display a string and then end
c Written to demonstrate Fortran program compilation
c
c Rafeeq Rehman, March 18, 2002

       program hello
       write (*,*) 'Hello world'
       stop
       end

Note that statements in Fortran start from column 7. All lines starting with c in column 1 are comment lines. You can compile this program using the following command:

g77 hello.for

The g77 compiler, like gcc, creates a.out as a default output executable. You can create output file with a different name using –o command line option. The following command creates “kaka” as output binary executable file.

g77 hello.for –o kaka

The g77 program uses many programs to generate output binary. You can display version information by using g77 –v command. Output of this command is shown below.

[rr@conformix 4]$ g77 -v
   g77 version 2.96 20000731 (Red Hat Linux 7.1 2.96-81) (from FSF-g77 version 0.5.26 
   20000731 (Red Hat Linux 7.1 2.96-81))
   Driving: g77 -v -c -xf77-version /dev/null -xnone
   Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
   gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81)
   /usr/lib/gcc-lib/i386-redhat-linux/2.96/tradcpp0 -lang-fortran -v -D__GNUC__=2 
   -D__GNUC_MINOR__=96 -D__GNUC_PATCHLEVEL__=0 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ 
   -D__linux__ -D__unix -D__linux -Asystem(posix) -Acpu(i386) -Amachine(i386) -Di386 
   -D__i386 -D__i386__ -D__tune_i386__ /dev/null /dev/null
   GNU traditional CPP version 2.96 20000731 (Red Hat Linux 7.1 2.96-81)
   /usr/lib/gcc-lib/i386-redhat-linux/2.96/f771 -fnull-version -quiet -dumpbase g77-version.
   f -version -fversion -o /tmp/ccr29cz4.s /dev/null
   GNU F77 version 2.96 20000731 (Red Hat Linux 7.1 2.96-81) (i386-redhat-linux) compiled by 
   GNU C version 2.96 20000731 (Red Hat Linux 7.1 2.96-81).
   GNU Fortran Front End version 0.5.26 20000731 (Red Hat Linux 7.1 2.96-81)
   as -V -Qy -o /tmp/ccqJAAN9.o /tmp/ccr29cz4.s
   GNU assembler version 2.10.91 (i386-redhat-linux) using BFD version 2.10.91.0.2
   ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o /tmp/ccijxxjb /tmp/ccqJAAN9.o /usr/
   lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o /usr/lib/gcc-lib/i386-redhat-linux/2.
   96/../../../crti.o /usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbegin.o -L/usr/lib/gcc-lib/
   i386-redhat-linux/2.96 -L/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../.. -lg2c -lm -lgcc 
   -lc -lgcc /usr/lib/gcc-lib/i386-redhat-linux/2.96/crtend.o /usr/lib/gcc-lib/
   i386-redhat-linux/2.96/../../../crtn.o
   /tmp/ccijxxjb
   __G77_LIBF77_VERSION__: 0.5.26 20000731 (prerelease)
   @(#)LIBF77 VERSION 19991115
   __G77_LIBI77_VERSION__: 0.5.26 20000731 (prerelease)
   @(#) LIBI77 VERSION pjw,dmg-mods 19991115
   __G77_LIBU77_VERSION__: 0.5.26 20000731 (prerelease)
   @(#) LIBU77 VERSION 19980709
   [rr@conformix 4]$
   

As you can see, it takes the help of gcc to compile programs. Note that Fortran programs have .for or .f at the end of the name of the program file.

As with gcc, you can create intermediate assembled or object code with g77. See manual pages of g77 for more information.


Next Section 12. Other Compilers
© 2003 Pearson Education, Inc. InformIT Division. All rights reserved.
800 East 96th Street Indianapolis, Indiana 46240