agnew@ucsd.edu (Robert A. Agnew) (03/07/89)
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
djones@decwrl.dec.com (Dave Jones) (03/07/89)
I haven't actually done it, but a very quick experiment using the -S flag and inspecting the assembly language seems to indicate that the following will work: procedure curse_proc(i,j: integer); external; procedure caller; begin curse_proc(1,2); end; To access the global variables defined in curses, you will have to write access-routines in C, because, so far as I know, pc has no concept of external variables. WARNING!! RELIGIOUS THEMES FOLLOW. The next question is, do you *really* want to use curses? Wouldn't Sunview be infinitely superior? For that matter, do you *really* want to use Pascal? Wouldn't C be infinitely superior? [[ Ya gotta do what ya gotta do... --wnl ]] ALL CLEAR. Anyway, good luck.. Dave J.