Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!deejay!gear!cadlab!staff From: staff@cadlab.sublink.ORG (Alex Martelli) Newsgroups: comp.lang.fortran Subject: Re: What is the FORTRAN for ? Keywords: FORTRAN Message-ID: <241@cadlab.sublink.ORG> Date: 5 Aug 90 09:25:05 GMT References: <1990Jul25.174153.16896@ecn.purdue.edu> <11029@chaph.usc.edu> <10583@batcomputer.tn.cornell.edu> Organization: CAD.LAB, Bologna, Italia Lines: 54 >There's no standard for calling sequences from C to FORTRAN, especially >for CHARACTER arguments. Our code has to run under VAX/VMS, IBM VM/CMS, >Ultrix, Unix, AIX, and so on--so portability is important, and cross >language calls are not currently portable. I built a set of C macros for this, which currently make C/Fortran calls with character parameters (and other tough cases, such as procedures as parameters) fully portable across the wide range of platforms CAD.LAB products support - all you mentioned, except CMS, plus OS/2, Apollo DOMAIN, etc. The syntax on the C side gets ugly, but it DOES work! Takes some days poring over the disassembled output of the Fortran compiler, that's all... For example: long FTN(example) (DCA(x), y, DCA(z) DCL(x) DCL(z)) DCHAL(x); long *y; DCHAL(z); { /* other local variables */ STCHA2(x, z); /* now you can use CHA(x), CHA(z) as the arrays of chars that you are passed, LEN(x) and LEN(z) as their int lengths (rvalues) */ } On many f77 compilers, DCA(x) is x, DCL(x) is ,L/**/x (note the comma and the token-pasting trick), DCHAL(x) is char *x; long L/**/x (no closing semicolon), STCHAn() is empty for all n, CHA(x) is x, LEN(x) is L/**/x. In other machines, token pasting is ANSI ##, and in some DCA(x) is x,L/**/x with DCL(x) empty; on VAX, DCHAL(x) is a pointer to a descriptor-structure; in OS/2, STCHAn() declares the L##x, which are NOT passed as arguments, AND initializes them from the global array where Microsoft Fortran hides them. Oh, and FTN() itself is "transparent" for Vax and HP, pastes an underline for Sun, attaches the "fortran" keyword for Microsoft, and so on and on. I also have macros for commons, etc. The ONLY thing I have not been able to make universally portable are functions returning single-precision reals; double-precision must be used (for PARAMETERS, no problems, since only a POINTER to them is passed). I have not considered the reverse interface, C calling Fortran, since we use C for "low-level" stuff and Fortran for application-level code, thus the C-calls-Fortran is a quite minor need for us. -- Alex Martelli - CAD.LAB s.p.a., v. Stalingrado 45, Bologna, Italia Email: (work:) staff@cadlab.sublink.org, (home:) alex@am.sublink.org Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; Fax: ++39 (51) 366964 (work only; any time of day or night).