Back to index

G95 on Linux

Introduction | The G95 compiler | PGPLOT | Sample Fortran code

Introduction

Here I describe how I got the G95 Fortran compiler up and running on Linux (Debian testing, but should work with most vaguely sensible distributions) with the PGPLOT library (useful for drawing graphs and such).


The G95 compiler

Getting it

The compiler itself can be found at the G95 project pages at Sourceforge. I opted for the Linux x86 Binary release, although sources are also available. If compiling from sources, you are advised to read the related documentation.

Unpacking and installing

Unpack the .tgz file in the usual way:

phunf@cricket:~$ tar -xzf g95-x86-linux.tgz phunf@cricket:~$ cd g95-install/ phunf@cricket:~/g95-install$ ls bin INSTALL lib phunf@cricket:~/g95-install$

I decided to install the compiler into /usr/local (you'll need to be root to do this), although it could just as easily go in your home directory providing that ~/bin is in $PATH.

phunf@cricket:~/g95-install$ su Password: <root password> cricket:/home/phunf/g95-install# cp bin/* /usr/local/bin/ cricket:/home/phunf/g95-install# cp lib/* /usr/local/lib/ -R cricket:/home/phunf/g95-install# exit phunf@cricket:~/g95-install$

As supplied in the binary package, the compiler is called i686-pc-linux-gnu-g95. To save typing, I symlinked it to /usr/local/bin/g95:

phunf@cricket:~$ su Password: <root password> cricket:/home/phunf# cd /usr/local/bin cricket:/usr/local/bin# ln -s i686-pc-linux-gnu-g95 g95 cricket:/usr/local/bin# exit phunf@cricket:~$ g95 g95: no input files phunf@cricket:~$

Testing

Here is hello.f90:

PROGRAM hello PRINT*,'Hello world' END PROGRAM hello

Compile & run:

phunf@cricket:~$ g95 -o hello hello.f90 phunf@cricket:~$ ./hello Hello world phunf@cricket:~$

As with most compilers, g95 takes a list of source files as parameters (in this case test.f90), along with any additional options (in this case, -o to specify an alternative output file).


PGPLOT

Getting and unpacking

PGPLOT is a graphical library sometimes used with Fortran. It produces output in a number of formats including Postscript, JPEG and PNG, as well as directly to an X window.

Download the source code from Caltech and unpack as before:

phunf@cricket:~$ tar -xzf pgplot5.2.tar.gz phunf@cricket:~$ cd pgplot/ phunf@cricket:~/pgplot$ ls aaaread.me makedoc sys_alliant sys_irix sys_vms applications makehelp sys_arc sys_linux sys_win (... loads of other files ...) phunf@cricket:~/pgplot$

Decide where to install PGPLOT. I chose /usr/local/pgplot but (as before) you'll need to be root to install it here. Create the installation directory:

phunf@cricket:~/pgplot$ su Password: <root password> cricket:/home/phunf/pgplot# mkdir /usr/local/pgplot cricket:/home/phunf/pgplot# chown phunf.users /usr/local/pgplot cricket:/home/phunf/pgplot# exit phunf@cricket:~/pgplot$

I also changed the ownership of the installation directory, to perform the rest of the installation as a non-root user.

Configuring and compiling

Before compilation, we need to decide which output formats/drivers we want to use with PGPLOT. Copy drivers.list from the PGPLOT source directory into the installation directory and open in your favourite text editor:

phunf@cricket:~/pgplot$ cp drivers.list /usr/local/pgplot/ phunf@cricket:~/pgplot$

The default output is NULL (no output). I added XWINDOW, XSERVE (both of which allow output to an X window) and PS (Postscript) by removing the exclamation mark from the beginning of the appropriate lines.

Next, we hit problems. PGPLOT doesn't have support for our compiler as standard, so we need to create a new configuration file for our setup. Thankfully this isn't too hard - we can just copy an existing one and change it a bit; I based mine on the NAGWare f95 file.

phunf@cricket:~/pgplot$ cd sys_linux phunf@cricket:~/pgplot/sys_linux$ cp f95_gcc.conf \ /usr/local/pgplot/local.conf phunf@cricket:~/pgplot/sys_linux$

The changes I made were:

Create a Makefile using the makemake utility supplied with PGPLOT.

phunf@cricket:~/pgplot$ cd /usr/local/pgplot/ phunf@cricket:/usr/local/pgplot$ ~/pgplot/makemake ~/pgplot linux For additional information, read file /home/phunf/pgplot/ sys_linux/aaaread.me Reading configuration file: ./local.conf Selecting uncommented drivers from ./drivers.list Found drivers NUDRIV PSDRIV XWDRIV Copying color database. Creating make file: makefile Determining object file dependencies. phunf@cricket:/usr/local/pgplot$

The moment of truth: type make at a shell prompt to set it going...

Testing PGPLOT

With a bit of luck, after a few minutes you'll get a message looking like this:

*** Finished compilation of PGPLOT *** Note that if you plan to install PGPLOT in a different directory than the current one, the following files will be needed. libpgplot.a libpgplot.so grfont.dat rgb.txt pgxwin_server Also note that subsequent usage of PGPLOT programs requires that the full path of the chosen installation directory be named in an environment variable named PGPLOT_DIR. phunf@cricket:/usr/local/pgplot$

Before reaching for the demos supplied with PGPLOT, you'll need to tell the dynamic linker where libpgplot.so is - this contains the compiled PGPLOT code. There are at least two ways of doing this:

Additionally (as the message helpfully points out) the PGPLOT_DIR variable should be set, so PGPLOT can find its fonts and helper programs. With all this in mind, you can try one of the demos by typing (in /usr/local/pgplot):

LD_LIBRARY_PATH=/usr/local/pgplot PGPLOT_DIR=/usr/local/pgplot \ ./pgdemo1

PGPLOT should respond with:

Graphics device/type (? to see list, default /NULL):

Typing ? gives the list:

PGPLOT v5.2.2 Copyright 1997 California Institute of Technology Interactive devices: /XWINDOW (X window window@node:display.screen/xw) /XSERVE (A /XWINDOW window that persists for re-use) Non-interactive file formats: /NULL (Null device, no output) /PS (PostScript file, landscape orientation) Graphics device/type (? to see list, default /NULL):

Pick /XWINDOW and you should be presented with a tasteful plot of y=x2. By repeatedly hitting ENTER in the terminal window, the demo cycles through a range of graphs, functions and shapes.

The other demo programs appear to test various aspects of PGPLOT as follows:

Tidying up

You can remove the .o files in the installation directory - they're only used during compilation.

phunf@cricket:/usr/local/pgplot$ rm *.o phunf@cricket:/usr/local/pgplot$

Sample Fortran code

graph.f90:

PROGRAM graph IMPLICIT NONE INTEGER :: i, ier, pgbeg REAL, DIMENSION(6) :: Xpoints, Ypoints ier=pgbeg(0, '?', 1, 1) IF (ier/=1) STOP CALL pgenv(0.0, 1.0, 0.0, 1.0, 0, 1) CALL pglab('X', 'Y', 'Graph of nothing in particular') DO i=0, 5, 1 IF (i/=0) THEN Xpoints(i+1)=i/5.0 Ypoints(i+1)=(i*i)/25.0 ELSE Xpoints(i+1)=0.0 Ypoints(i+1)=0.0 END IF PRINT*,i,Xpoints(i+1),Ypoints(i+1) END DO CALL pgline(SIZE(Xpoints, 1), Xpoints, Ypoints) CALL pgend END PROGRAM graph

To compile:

g95 -o graph graph.f90 -L/usr/X11R6/lib -L/usr/local/pgplot -lX11 \ -lpgplot

Note how we link with the PGPLOT and X11 libraries. Then to run:

LD_LIBRARY_PATH=/usr/local/pgplot PGPLOT_DIR=/usr/local/pgplot \ ./graph

Tada. Let me know if you encounter any problems during compiling/installing, and I'll try to update this page with any common issues and their solutions.