🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.sun
Article #11309

Re: Pascal calling 'C'

#11309
From: Robert A. Agnew
Date: Mon, 06 Mar 1989 22:35
58 lines
3258 bytes
gemed!altair_4!doug@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@trwrc.rc.trw.com
Technical Details
Message-ID: <325@trwrc.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!husc6!rice!sun-spots-request
References: <8902131755.AA04082@altair_4.gemed.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!husc6!rice!sun-spots-request
From: trwrc!hp-sdd!agnew@ucsd.edu (Robert A. Agnew)
Newsgroups: comp.sys.sun
Subject: Re: Pascal calling 'C'
Keywords: Software
Message-ID: <325@trwrc.UUCP>
Date: 6 Mar 89 22:35:30 GMT
References: <8902131755.AA04082@altair_4.gemed.com>
Sender: usenet@rice.edu
Organization: TRW-MEAD, San Diego, Ca.
Lines: 58
Approved: Sun-Spots@rice.edu
Original-Date: 22 Feb 89 20:26:21 GMT
X-Sun-Spots-Digest: Volume 7, Issue 183, message 1 of 17