Compiling



To use Slatec in C, your computer needs a Fortran and a C compiler. You may never need to call the Fortran compiler, but the C compiler (rather its linker) requires a standard Fortran library ( say, xlf90 , if we use an IBM XL Fortran Compiler ) which Slatec functions rely on.

Assuming you know what compilers and library names are apropriate on your system, you only need to tell the C compiler to link with the Slatec library and the standard Fortran library. For example:

cc prog.c -lxlf90 -lslatec
where cc is the C compiler (and linker), -l is the prefix for linking a library, xlf90 is your standard Fortran library, slatec is the name of your Slatec library, and prog.c is your C program.


You may also compile your C code into object files first (usually with cc -c), and then use the Fortran compiler to do the linking:

cc -c prog.c
f77 prog.o -lslatec
Here you are not required to indicate the linking of the standard Fortran library since f77 will automatically include it!!



Before proceeding to compiling your first program with a Slatec function call, you should have compiled and executed the simple example in the section 1. Calling a Slatec function so that you are certain of your C compiler's calling convention before involving the Slatec library.

Let us now take a simple Slatec function cpqr79(..). This function finds the zeros of a polynomial. The comments in the source code, cpqr79.f, contains some documentation about this function:

      SUBROUTINE CPQR79 (NDEG, COEFF, ROOT, IERR, WORK)

C   Abstract
C       This routine computes all zeros of a polynomial of degree NDEG
C       with complex coefficients by computing the eigenvalues of the
C       companion matrix.
C
C   Description of Parameters
C       The user must dimension all arrays appearing in the call list
C            COEFF(NDEG+1), ROOT(NDEG), WORK(2*NDEG*(NDEG+1))
C
C    --Input--
C      NDEG    degree of polynomial
C
C      COEFF   COMPLEX coefficients in descending order.  i.e.,
C              P(Z)= COEFF(1)*(Z**NDEG) + COEFF(NDEG)*Z + COEFF(NDEG+1)
C
C      WORK    REAL work array of dimension at least 2*NDEG*(NDEG+1)
C
C   --Output--
C      ROOT    COMPLEX vector of roots
C
C      IERR    Output Error Code


Here is a short C program that we will use to test our ability to compile C programs that call the Slatec library. For the commented version of this program click here.

#include <stdio.h>
#include <math.h>
#define REAL float
#define DD 	48

struct complex {REAL re; REAL im;};

void cpqr79(int*, struct complex*, struct complex*,int*,REAL*);

int main() {
  int ndeg[1];
  int ierr[1];
  struct complex coeff[3];
  struct complex root[2];
  REAL work[DD];
  int i=0;
  REAL f;
 coeff[0].re=1.; coeff[0].im=0.; 	
 coeff[1].re=3.; coeff[1].im=0.; 	
 coeff[2].re=2.; coeff[2].im=0.; 	
 ndeg[0]=2;  

 cpqr79(ndeg, coeff, root, ierr, work);
  
 for (i=0;i<ndeg[0];i++ ) printf("(%g,%g).",root[i].re,root[i].im);
 if (ierr[0]) printf("Error Flag = %d\n",ierr[0]);
 puts("\n"); return 0;			  
} /* Try compiling with cc simpleCPQ.c -lm -lslatec -lxlf90 */
 

Try compiling the code above with either of the two methods described above.



Back to Main