Previous: Writing C code Directly in CL Files Up: A Programmer's Guide for AKCL and MAXIMA Next: Maintaining AKCL and Maxima

Loading C-coded Functions into CL or Maxima

The mechanism outlined in the previous section is recommended for including codes writing in another language into maxima (AKCL). However, there is also a mechanism similar to the Franz cfasl available in maxima (AKCL).

Basically, this involves a compiled C file cfile.o (by cc) and a lisp wrapper file lispfile.o (by lc or mc). The wrapper defines lisp functions that interface to the C-coded functions. These two files are loaded into akcl or maxima with the lisp function si::faslink.

Here is an example of defining a function similar to fopen for akcl. The C file is process.c and the lisp file is proc.lsp.


/********************* file process.c  *******************************/
#include <stdio.h>

static FILE *pstm;

process(cmd, mode) /* run process , mode must be "w" */ char *cmd; char *mode; { pstm = popen(cmd, mode); if ( pstm == NULL) return(1); return(0); }

prwrite(str) /* write to stdin of process */ char *str; { fprintf(pstm, "%s\n", str); fflush(pstm); return(0); }

prclose() /* close stream to process */ { fclose(pstm); return(0); }

Now the lisp interface file is as follows.

(in-package 'system)

;; defines lisp function process to access C function process ;; by passing two lisp string args to the C function and ;; receives an integer return value (defentry process (string string) (int process))

;; these are similar (defentry prwrite (string) (int prwrite)) (defentry prclose () (int prclose))

The above two files can be compiles into object files individually. Then they can be load into akcl or maxima with


(si:faslink "proc.o" "process.o -lc")
where the library specification for -lc is not really needed.

The AKCL/BSD specific si:faslink is given in the form


(si:faslink file string)
It loads the lisp compiled file (called a fasl file) while linking the object files and libraries specified by string.

farrell@mcs.kent.edu