📄 cfortran.html
字号:
<html><head><title>cfortran.h: Interfacing C or C++ and FORTRAN</title></head><body bgcolor="#ffffff">
<table border="0" cellpadding="5" cellspacing="0" width="100%">
<tbody><tr bgcolor="#0000ff"><td><a href="http://www.cern.ch/">
<img src="cfortran_files/cernbanner.gif" border="0"></a></td></tr></tbody></table>
<h1>cfortran.h: Interfacing C or C++ and <i>FORTRAN</i></h1>
<hr>
<b>Author:</b><a href="http://www-zeus.desy.de/%7Eburow">Burkhard Burow</a> <br>
<b>Email:</b> burow@desy.de <br>
<b>www:</b> <a href="http://www-zeus.desy.de/%7Eburow/cfortran">
www-zeus.desy.de/~burow/cfortran</a> <br>
<hr>
<p>
<b>Supports:</b>
<font color="#993300"><pre> Alpha and VAX VMS, Alpha OSF, DECstation and VAX Ultrix, IBM RS/6000,
Silicon Graphics, Sun, CRAY, Apollo, HP9000, LynxOS, Convex, Absoft,
f2c, g77, NAG f90, PowerStation <i>FORTRAN</i> with Visual C++, NEC SX-4,
Portland Group.
</pre></font>
C and C++ are generally equivalent as far as <tt>cfortran.h</tt> is concerned.
Unless explicitly noted otherwise, mention of C implicitly includes C++.
C++ compilers tested include:
</p><p><font color="#993300"><pre> SunOS> CC +p +w # Clean compiles.
IRIX> CC # Clean compiles.
IRIX> CC -fullwarn # Still some warnings to be overcome.
GNU> g++ -Wall # Compiles are clean, other than warnings for unused
# cfortran.h static routines.
</pre></font>
<b>N.B.</b>: The best documentation on interfacing C or C++ and <i>FORTRAN</i> is in
the chapter named something like 'Interfacing C and <i>FORTRAN</i>'
to be found in the user's guide of almost every <i>FORTRAN</i> compiler.
Understanding this information for one or more <i>FORTRAN</i> compilers
greatly clarifies the aims and actions of <tt>cfortran.h</tt>.
Such a chapter generally also addresses issues orthogonal to <tt>cfortran.h</tt>,
for example the order of array indices, the index of the first element,
as well as compiling and linking issues.
</p><h2> Short Summary of the Syntax Required to Create the Interface</h2>
e.g. Prototyping a <i>FORTRAN</i> subroutine for C:
<tt>PROTOCCALLSFSUBni</tt> is optional for C, but mandatory for C++.
<font color="#993300"></font><pre><font color="#993300"> PROTOCCALLSFSUB2(SUB_NAME,sub_name,STRING,PINT)
#define SUB_NAME(A,B) CCALLSFSUB2(SUB_NAME,sub_name,STRING,PINT, A,B)
^ - -
number of arguments _____| | STRING BYTE PBYTE BYTEV(..)|
/ | STRINGV DOUBLE PDOUBLE DOUBLEV(..)|
/ | PSTRING FLOAT PFLOAT FLOATV(..)|
types of arguments ____ / | PNSTRING INT PINT INTV(..)|
\ | PPSTRING LOGICAL PLOGICAL LOGICALV(..)|
\ | PSTRINGV LONG PLONG LONGV(..)|
\ | ZTRINGV SHORT PSHORT SHORTV(..)|
| PZTRINGV ROUTINE PVOID SIMPLE |
- -
</font></pre>
e.g. Prototyping a <i>FORTRAN</i> function for C:
<font color="#993300"><pre>/* PROTOCCALLSFFUNn is mandatory for both C and C++. */
PROTOCCALLSFFUN1(INT,FUN_NAME,fun_name,STRING)
#define FUN_NAME(A) CCALLSFFUN1(FUN_NAME,fun_name,STRING, A)
</pre></font>
e.g. calling <tt>FUN_NAME</tt> from C:
<font color="#993300"><pre> {int a; a = FUN_NAME("hello");}
</pre></font>
e.g. Creating a <i>FORTRAN</i>-callable wrapper for
a C function returning void, with a 7 dimensional integer array argument:
[Not supported from C++.]
<font color="#993300"><pre>FCALLSCSUB1(csub_name,CSUB_NAME,csub_name,INTVVVVVVV)
</pre></font>
e.g. Creating a <i>FORTRAN</i>-callable wrapper for other C functions:
<font color="#993300"><pre>FCALLSCFUN1(STRING,cfun_name,CFUN_NAME,cfun_name,INT)
[ ^-- BYTE, DOUBLE, FLOAT, INT, LOGICAL, LONG, SHORT, VOID
are other types returned by functions. ]
</pre></font>
e.g. COMMON BLOCKs:
<font color="#993300"><pre><b>FORTRAN:</b>
common /fcb/ v,w,x
character *(13) v, w(4), x(3,2)
<b>C:</b>
typedef struct { char v[13],w[4][13],x[2][3][13]; } FCB_DEF;
#define FCB COMMON_BLOCK(FCB,fcb)
COMMON_BLOCK_DEF(FCB_DEF,FCB);
FCB_DEF FCB; /* Define, i.e. allocate memory, in exactly one *.c file. */
</pre></font>
e.g. accessing <tt>FCB</tt> in C:
<font color="#993300"><pre> printf("%.13s",FCB.v);
</pre></font>
<h2> I) Introduction</h2>
<tt>cfortran.h</tt> is an easy-to-use powerful bridge between C and <i>FORTRAN</i>.
It provides a completely transparent, machine independent interface between
C and <i>FORTRAN</i> routines (= subroutines and/or functions) and global data,
i.e. structures and COMMON blocks.
<p>
The complete <tt>cfortran.h</tt> package consists of 4 files: the documentation in
<tt>cfortran.doc</tt>, the engine <tt>cfortran.h</tt>, examples in <tt>cfortest.c</tt> and
<tt>cfortex.f</tt>/or. [<tt>cfortex.for</tt> under VMS,
<tt>cfortex.f</tt> on other machines.]
</p><p>
The <tt>cfortran.h</tt> package continues to be developed.
The most recent version is
available via WWW at
<tt><a href="http://www-zeus.desy.de/%7Eburow/cfortran">
http://www-zeus.desy.de/~burow/cfortran</a></tt>.
</p><p>
The examples may be run using one of the following sets of instructions:
</p><p>
<b>N.B.</b> Unlike earlier versions, <tt>cfortran.h</tt> 3.0 and later versions
automatically uses the correct <tt>ANSI ##</tt> or <tt>pre-ANSI /**/</tt>
preprocessor operator as required by the C compiler.
</p><p>
<b>N.B.</b> As a general rule when trying to determine how to link C and
<i>FORTRAN</i>,
link a trivial <i>FORTRAN</i> program using the <i>FORTRAN</i> compilers verbose option,
in order to see how the <i>FORTRAN</i> compiler drives the linker. e.g.
<font color="#993300"><pre> unix> cat f.f
END
unix> f77 -v f.f
.. lots of info. follows ...
</pre></font>
</p><p>
<b>N.B.</b> If using a C <tt>main()</tt>, i.e. <i>FORTRAN</i> <tt>PROGRAM</tt>
is not entry of the executable,
and if the link bombs with a complaint about
a missing "<tt>MAIN</tt>" (e.g. <tt>MAIN__</tt>, <tt>MAIN_</tt>,
<tt>f90_main</tt> or similar),
then <i>FORTRAN</i> has hijacked the entry point to the executable
and wishes to call the rest of the executable via "<tt>MAIN</tt>".
This can usually be satisfied by doing e.g. '<tt>cc -Dmain=MAIN__ ...</tt>'
but often kills the command line arguments in <tt>argv</tt> and <tt>argc</tt>.
The <tt>f77</tt> verbose option, usually <tt>-v</tt>, may point to a solution.
<font color="#993300"></font></p><pre><font color="#993300">RS/6000> # Users are strongly urged to use f77 -qextname and cc -Dextname
RS/6000> # Use -Dextname=extname if extname is a symbol used in the C code.
RS/6000> xlf -c -qextname cfortex.f
RS/6000> cc -c -Dextname cfortest.c
RS/6000> xlf -o cfortest cfortest.o cfortex.o && cfortest
DECFortran> #Only DECstations with DECFortran for Ultrix RISC Systems.
DECFortran> cc -c -DDECFortran cfortest.c
DECFortran> f77 -o cfortest cfortest.o cfortex.f && cfortest
IRIX xxxxxx 5.2 02282015 IP20 mips
MIPS> # DECstations and Silicon Graphics using the MIPS compilers.
MIPS> cc -o cfortest cfortest.c cfortex.f -lI77 -lU77 -lF77 && cfortest
MIPS> # Can also let f77 drive linking, e.g.
MIPS> cc -c cfortest.c
MIPS> f77 -o cfortest cfortest.o cfortex.f && cfortest
Apollo> # Some 'C compiler 68K Rev6.8' break. <a href="http://wwwasd.web.cern.ch/wwwasd/cernlib/cfortran.html#SIIoApollo">[See Section II o) Notes: Apollo]</a>
Apollo> f77 -c cfortex.f && cc -o cfortest cfortest.c cfortex.o && cfortest
VMS> define lnk$library sys$library:vaxcrtl
VMS> cc cfortest.c
VMS> fortran cfortex.for
VMS> link/exec=cfortest cfortest,cfortex
VMS> run cfortest
OSF1 xxxxxx V3.0 347 alpha
Alpha/OSF> # Probably better to let cc drive linking, e.g.
Alpha/OSF> f77 -c cfortex.f
Alpha/OSF> cc -o cfortest cfortest.c cfortex.o -lUfor -lfor -lFutil -lots -lm
Alpha/OSF> cfortest
Alpha/OSF> # Else may need 'cc -Dmain=MAIN__' to let f77 drive linking.
Sun> # Some old cc(1) need a little help. <a href="http://wwwasd.web.cern.ch/wwwasd/cernlib/cfortran.html#SIIoSun">[See Section II o) Notes: Sun]</a>
Sun> f77 -o cfortest cfortest.c cfortex.f -lc -lm && cfortest
Sun> # Some older f77 may require 'cc -Dmain=MAIN_'.
CRAY> cft77 cfortex.f
CRAY> cc -c cfortest.c
CRAY> segldr -o cfortest.e cfortest.o cfortex.o
CRAY> ./cfortest.e
NEC> cc -c -Xa cfortest.c
NEC> f77 -o cfortest cfortest.o cfortex.f && cfortest
VAX/Ultrix/cc> # For cc on VAX Ultrix only, do the following once to cfortran.h.
VAX/Ultrix/cc> mv cfortran.h cftmp.h && grep -v "^#pragma" <cftmp.h>cfortran.h
VAX/Ultrix/f77> # In the following, 'CC' is either 'cc' or 'gcc -ansi'. NOT'vcc'
VAX/Ultrix/f77> CC -c -Dmain=MAIN_ cfortest.c
VAX/Ultrix/f77> f77 -o cfortest cfortex.f cfortest.o && cfortest
LynxOS> # In the following, 'CC' is either 'cc' or 'gcc -ansi'.
LynxOS> # Unfortunately cc is easily overwhelmed by cfortran.h,
LynxOS> # and won't compile some of the cfortest.c demos.
LynxOS> f2c -R cfortex.f
LynxOS> CC -Dlynx -o cfortest cfortest.c cfortex.c -lf2c && cfortest
HP9000> # Tested with HP-UX 7.05 B 9000/380 and with A.08.07 A 9000/730
HP9000> # CC may be either 'c89 -Aa' or 'cc -Aa'
HP9000> # Depending on the compiler version, you may need to include the
HP9000> # option '-tp,/lib/cpp' or worse, you'll have to stick to the K&R C.
HP9000> # <a href="http://wwwasd.web.cern.ch/wwwasd/cernlib/cfortran.html#SIIoHP9000">[See Section II o) Notes: HP9000]</a>
HP9000> # Users are strongly urged to use f77 +ppu and cc -Dextname
HP9000> # Use -Dextname=extname if extname is a symbol used in the C code.
HP9000> CC -Dextname -c cfortest.c
HP9000> f77 +ppu cfortex.f -o cfortest cfortest.o && cfortest
HP9000> # Older f77 may need
HP9000> f77 -c cfortex.f
HP9000> CC -o cfortest cfortest.c cfortex.o -lI77 -lF77 && cfortest
HP0000> # If old-style f77 +800 compiled objects are required:
HP9000> # #define hpuxFortran800
HP9000> cc -c -Aa -DhpuxFortran800 cfortest.c
HP9000> f77 +800 -o cfortest cfortest.o cfortex.f
f2c> # In the following, 'CC' is any C compiler.
f2c> f2c -R cfortex.f
f2c> CC -o cfortest -Df2cFortran cfortest.c cfortex.c -lf2c && cfortest
Portland Group $ # Presumably other C compilers also work.
Portland Group $ pgcc -DpgiFortran -c cfortest.c
Portland Group $ pgf77 -o cfortest cfortex.f cfortest.o && cfortest
NAGf90> # cfortex.f is distributed with <i>FORTRAN</i> 77 style comments.
NAGf90> # To convert to f90 style comments do the following once to cfortex.f:
NAGf90> mv cfortex.f cf_temp.f && sed 's/^C/\!/g' cf_temp.f > cfortex.f
NAGf90> # In the following, 'CC' is any C compiler.
NAGf90> CC -c -DNAGf90Fortran cfortest.c
NAGf90> f90 -o cfortest cfortest.o cfortex.f && cfortest
PC> # On a PC with PowerStation <i>FORTRAN</i> and Visual_C++
PC> cl /c cftest.c
PC> fl32 cftest.obj cftex.for
GNU> # GNU <i>FORTRAN</i>
GNU> # <a href="http://wwwasd.web.cern.ch/wwwasd/cernlib/cfortran.html#gcctrad">See Section VI caveat on using 'gcc -traditional'</a>.
GNU> gcc -ansi -Wall -O -c -Df2cFortran cfortest.c
GNU> g77 -ff2c -o cfortest cfortest.o cfortex.f && cfortest
AbsoftUNIX> # Absoft <i>FORTRAN</i> for all UNIX based operating systems.
AbsoftUNIX> # e.g. Linux or Next on Intel or Motorola68000.
AbsoftUNIX> # Absoft f77 -k allows <i>FORTRAN</i> routines to be safely called from C.
AbsoftUNIX> gcc -ansi -Wall -O -c -DAbsoftUNIXFortran cfortest.c
AbsoftUNIX> f77 -k -o cfortest cfortest.o cfortex.f && cfortest
AbsoftPro> # Absoft Pro <i>FORTRAN</i> for MacOS
AbsoftPro> # Use #define AbsoftProFortran
CLIPPER> # INTERGRAPH CLIX using CLIPPER C and <i>FORTRAN</i> compilers.
CLIPPER> # N.B. - User, not cfortran.h, is responsible for
CLIPPER> # f77initio() and f77uninitio() if required.
CLIPPER> # - LOGICAL values are not mentioned in CLIPPER doc.s,
CLIPPER> # so they may not yet be correct in cfortran.h.
CLIPPER> # - K&R mode (-knr or Ac=knr) breaks FLOAT functions
CLIPPER> # (see CLIPPER doc.s) and cfortran.h does not fix it up.
CLIPPER> # [cfortran.h ok for old sun C which made the same mistake.]
CLIPPER> acc cfortest.c -c -DCLIPPERFortran
CLIPPER> af77 cfortex.f cfortest.o -o cfortest
</cftmp.h></font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -