From: kruel@phys-chemie.uni-wuerzburg.dbp.de (Thomas-Martin Kruel)
Newsgroups: sci.math.num-analysis
Subject: Re: Can you call IMSL from C program?
Summary: It can be done quite easily
Keywords: IMSL
Date: 30 Jul 92 19:38:14 GMT
Organization: Institut fuer Physikalische Chemie / Uni Wuerzburg

In article <9246@emory.mathcs.emory.edu> riddle@mathcs.emory.edu (Larry Riddle) writes:
>I am posting this message for a friend. She would like to call
>some of the IMSL routines from a C program. I suspect this should
>be possible, but I'm not an IMSL expert nor a C expert. Can this
>be done, and if so, how? She would be working on a Sun 4.
>Any pointers (no pun intended) would be appreciated. Thank you.
>

Well, this is quite easy. Here is an example how it might work:

Here some excerpts from a fictive C program:
/* declaration part: */
 int     npt, nrows, ncols;
 float   tau;
 float   x[50];
 double  *workarray;
 char    name[40];

 nrows = 1000;
 ncols = 100;
 workarray = (double *) calloc (nrows*ncols, sizeof(double);

 /* calling the FORTRAN subroutine: */
 fortprog_(&tau, &name[0], &npt, &x[0], workarray, &nrows);

------
and the corresponding FORTRAN subroutine would look like this:

      subroutine fortprog (tau, name, npt, x, workarray, nrows)
 
      integer*4  npt, nrows
      real*4     tau, x(50)
      real*8     workarray(nrows,1)
      character  name*40

You should note the variables are handed over via their *addresses*, f.e.
"tau" is handend over from the C program to the FORTRAN subroutine via "&tau".

Notice also the automatic workspace allocation for "workarray", were it is
allocated in the C program as an array of nrows*ncols elements, handed over
to the FORTRAN subroutine via the pointer to the base address of the
reserved memory, and interpreted in the FORTRAN subroutine as a 2-dimensional 
matrix with leading dimension "nrows", which must be handed over, also.

Have a look also how the statically dimensioned array "x" and the character
array "name" are handed over.

Instead of "fortprog", there could be an IMSL routine in place.

In SUN C/FORTRAN, the subroutine called MUST have a trailing underscore
i.e. "fortprog_" in order to get the linkage run. So, if you call "EVCSF" from
IMSL, you must say "evcsf_(..)" in your C program. This is also true for 
DEC, but not on HP's. I don't know for other systems. 

You must ensure that the types and lengths of variables conform, i.e.
a REAL*8 must be declared as DOUBLE in the C routine, *not* FLOAT. This is
trivial, but can cause lot of trouble to beginners (Segmentation fault.).

Compiling and linking will perform as follows:

   cc -c main.c
   f77 -c -O3 fortprog.f
   f77 -o myprogram main.o fortprog.o

You need f77 for the linkage to include the appropriate FORTRAN libs
(of course).

In case that "fortprog" were a module from IMSL, you'd replace the procedure
by:
   cc -c main.c
   f77 -o myprogram main.o -limsl

That's all. I do it all day with IMSL 9.2 and IMSL 11.0 on a SUN. Works fine.

Hope that helps,
    Thomas.
-- 

Th.-M. Kruel   Institut fuer Physikalische Chemie der Universitaet Wuerzburg
               Marcusstr. 9-11,  D-8700 Wuerzburg (Germany)
               Phone: +49 931 31579