Pascal calling 'C'

Robert A. Agnew trwrc!hp-sdd!agnew at ucsd.edu
Tue Mar 7 08:35:30 AEST 1989


gemed!altair_4!doug at central.sun.com (Doug Curtis x4-6826) writes:
> I've been having problems trying to call 'C' routines from a pascal
> program.  If there's anyone out there that has had experience in doing
> this I would appreciate some help. I'm primarily interested in calling
> curses routines.  I am currently using the 3.4 version of SUNOS.

Sure, just read your Sun Pascal manual chapter 6 " The Pascal -- C
Interface".  I do it all the time. Just remember that pointers in C are
VAR parameters in Pascal, others (by value) are not. Reals are double etc.
For instance I often use the C math routines by including the following
statements:

function power(x,y: real): real; external;
function jn(n: integer; x: real): real; external;
function gamma(x: real): real; external;
function lgamma(x: real): real; external;
function Gamma(x: real): real; external;
function asin(x: real): real; external;
function acos(x: real): real; external;
function tan(x: real): real; external;
function atan(x: real): real; external;
function tanh(x: real): real; external;
function atanh(x: real): real; external;
function sinh(x: real): real; external;
function asinh(x: real): real; external;
function cosh(x: real): real; external;
function acosh(x: real): real; external;
function atan2(y,x: real): real; external;
function arctan2(y,x: real): real; external;
function log(x: real): real; external;
function log10(x: real): real; external;
function exp10(x: real): real; external;

Actually these are in a ".h" file under /usr/include/Pascal. To get a
little trickier, one can use the C exit function from Pascal by including
"procedure exit(i: integer); external;" and executing a exit(0) or
exit(n). What's slicker is to execute any Unix command by declaring
"procedure system(s: String); external;" and executing
system(NewString('lpr myfile.tst')) for instance. You can compile your C
routines separately and compile them with the pascal programs as in

pc myprog.p mycobj.o

or you may put them in a library along with object modules in Ada and
Fortran etc. and compile them with

pc myprog.p -lmylib

where your library is /usr/local/lib/libmylib.a  If you don't have SU
privilege then you must use the "L" loader switch in pc. If you are using
curses, don't forget the " -lcurses -ltermcap" arguments to pc.  It may be
worth your time to write a c module which re-exports the equivalent Pascal
routine. The only real problems that I have run into are trying to use C
functions with variable numbers of arguments and with environment
variables. There are always work-arounds though.

Bob Agnew 	usenet:	hplabs!hp-sdd!trwrc!agnew
		internet: agnew at trwrc.rc.trw.com



More information about the Comp.sys.sun mailing list