⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfortran.html

📁 Cfortran is useful for mixing language programing when you want call some C function in your FORTRAN
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<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&gt; CC +p +w      # Clean compiles.
  IRIX&gt;  CC            # Clean compiles.
  IRIX&gt;  CC -fullwarn  # Still some warnings to be overcome.
  GNU&gt;   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&gt; cat f.f
                END
       unix&gt; 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&gt; # Users are strongly urged to use f77 -qextname and cc -Dextname
RS/6000&gt; # Use -Dextname=extname if extname is a symbol used in the C code.
RS/6000&gt; xlf -c -qextname cfortex.f
RS/6000&gt; cc  -c -Dextname cfortest.c
RS/6000&gt; xlf -o cfortest cfortest.o cfortex.o &amp;&amp; cfortest 

DECFortran&gt; #Only DECstations with DECFortran for Ultrix RISC Systems.
DECFortran&gt; cc -c -DDECFortran cfortest.c
DECFortran&gt; f77 -o cfortest cfortest.o cfortex.f  &amp;&amp;  cfortest

IRIX xxxxxx 5.2 02282015 IP20 mips
MIPS&gt; # DECstations and Silicon Graphics using the MIPS compilers.
MIPS&gt; cc -o cfortest cfortest.c cfortex.f -lI77 -lU77 -lF77  &amp;&amp;  cfortest
MIPS&gt; # Can also let f77 drive linking, e.g.
MIPS&gt; cc -c cfortest.c
MIPS&gt; f77 -o cfortest cfortest.o cfortex.f  &amp;&amp;  cfortest

Apollo&gt; # 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&gt; f77 -c cfortex.f &amp;&amp; cc -o cfortest cfortest.c cfortex.o  &amp;&amp;  cfortest

VMS&gt; define lnk$library sys$library:vaxcrtl
VMS&gt; cc cfortest.c
VMS&gt; fortran cfortex.for
VMS&gt; link/exec=cfortest cfortest,cfortex
VMS&gt; run cfortest

OSF1 xxxxxx V3.0 347 alpha
Alpha/OSF&gt; # Probably better to let cc drive linking, e.g.
Alpha/OSF&gt; f77 -c cfortex.f
Alpha/OSF&gt; cc  -o cfortest cfortest.c cfortex.o -lUfor -lfor -lFutil -lots -lm
Alpha/OSF&gt; cfortest
Alpha/OSF&gt; # Else may need 'cc -Dmain=MAIN__' to let f77 drive linking.

Sun&gt; # 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&gt; f77 -o cfortest cfortest.c cfortex.f -lc -lm  &amp;&amp;  cfortest
Sun&gt; # Some older f77 may require 'cc -Dmain=MAIN_'.

CRAY&gt; cft77 cfortex.f
CRAY&gt; cc -c cfortest.c
CRAY&gt; segldr -o cfortest.e cfortest.o cfortex.o
CRAY&gt; ./cfortest.e

NEC&gt; cc -c -Xa cfortest.c
NEC&gt; f77 -o cfortest cfortest.o cfortex.f  &amp;&amp;  cfortest

VAX/Ultrix/cc&gt; # For cc on VAX Ultrix only, do the following once to cfortran.h.
VAX/Ultrix/cc&gt; mv cfortran.h cftmp.h &amp;&amp; grep -v "^#pragma" <cftmp.h>cfortran.h
                                            
VAX/Ultrix/f77&gt; # In the following, 'CC' is either 'cc' or 'gcc -ansi'. NOT'vcc'
VAX/Ultrix/f77&gt; CC -c -Dmain=MAIN_ cfortest.c
VAX/Ultrix/f77&gt; f77 -o cfortest cfortex.f cfortest.o  &amp;&amp;  cfortest

LynxOS&gt; # In the following, 'CC' is either 'cc' or 'gcc -ansi'.
LynxOS&gt; # Unfortunately cc is easily overwhelmed by cfortran.h,
LynxOS&gt; #  and won't compile some of the cfortest.c demos.
LynxOS&gt; f2c -R cfortex.f
LynxOS&gt; CC -Dlynx -o cfortest cfortest.c cfortex.c -lf2c  &amp;&amp;  cfortest

HP9000&gt; # Tested with HP-UX 7.05 B 9000/380 and with A.08.07 A 9000/730
HP9000&gt; # CC may be either 'c89 -Aa' or 'cc -Aa'
HP9000&gt; #    Depending on the compiler version, you may need to include the
HP9000&gt; #    option '-tp,/lib/cpp' or worse, you'll have to stick to the K&amp;R C.
HP9000&gt; #    <a href="http://wwwasd.web.cern.ch/wwwasd/cernlib/cfortran.html#SIIoHP9000">[See Section II o) Notes: HP9000]</a>
HP9000&gt; # Users are strongly urged to use f77 +ppu and cc -Dextname
HP9000&gt; # Use -Dextname=extname if extname is a symbol used in the C code.
HP9000&gt; CC  -Dextname -c cfortest.c
HP9000&gt; f77 +ppu         cfortex.f  -o cfortest cfortest.o &amp;&amp; cfortest
HP9000&gt; # Older f77 may need
HP9000&gt; f77 -c cfortex.f
HP9000&gt; CC -o cfortest cfortest.c cfortex.o -lI77 -lF77 &amp;&amp; cfortest

HP0000&gt; # If old-style f77 +800 compiled objects are required:
HP9000&gt; # #define hpuxFortran800
HP9000&gt; cc -c -Aa -DhpuxFortran800 cfortest.c
HP9000&gt; f77 +800 -o cfortest cfortest.o cfortex.f

f2c&gt; # In the following, 'CC' is any C compiler.
f2c&gt; f2c -R cfortex.f
f2c&gt; CC -o cfortest -Df2cFortran cfortest.c cfortex.c -lf2c  &amp;&amp;  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 &amp;&amp; cfortest

NAGf90&gt; # cfortex.f is distributed with <i>FORTRAN</i> 77 style comments.
NAGf90&gt; # To convert to f90 style comments do the following once to cfortex.f: 
NAGf90&gt; mv cfortex.f cf_temp.f &amp;&amp; sed 's/^C/\!/g' cf_temp.f &gt; cfortex.f
NAGf90&gt; # In the following, 'CC' is any C compiler.
NAGf90&gt; CC -c -DNAGf90Fortran cfortest.c
NAGf90&gt; f90 -o cfortest cfortest.o cfortex.f &amp;&amp;  cfortest

PC&gt; # On a PC with PowerStation <i>FORTRAN</i> and Visual_C++
PC&gt; cl /c cftest.c
PC&gt; fl32  cftest.obj cftex.for

GNU&gt; # GNU <i>FORTRAN</i>
GNU&gt; # <a href="http://wwwasd.web.cern.ch/wwwasd/cernlib/cfortran.html#gcctrad">See Section VI caveat on using 'gcc -traditional'</a>.
GNU&gt; gcc -ansi -Wall -O -c -Df2cFortran cfortest.c
GNU&gt; g77 -ff2c -o cfortest cfortest.o cfortex.f &amp;&amp;  cfortest

AbsoftUNIX&gt; # Absoft <i>FORTRAN</i> for all UNIX based operating systems.
AbsoftUNIX&gt; # e.g. Linux or Next on Intel or Motorola68000.
AbsoftUNIX&gt; # Absoft f77 -k allows <i>FORTRAN</i> routines to be safely called from C.
AbsoftUNIX&gt; gcc -ansi -Wall -O -c -DAbsoftUNIXFortran cfortest.c
AbsoftUNIX&gt; f77 -k -o cfortest cfortest.o cfortex.f &amp;&amp; cfortest

AbsoftPro&gt; # Absoft Pro <i>FORTRAN</i> for MacOS
AbsoftPro&gt; # Use #define AbsoftProFortran

CLIPPER&gt; # INTERGRAPH CLIX using CLIPPER C and <i>FORTRAN</i> compilers.
CLIPPER&gt; # N.B. - User, not cfortran.h, is responsible for
CLIPPER&gt; #        f77initio() and f77uninitio() if required.
CLIPPER&gt; #      - LOGICAL values are not mentioned in CLIPPER doc.s,
CLIPPER&gt; #        so they may not yet be correct in cfortran.h.
CLIPPER&gt; #      - K&amp;R mode (-knr or Ac=knr) breaks FLOAT functions
CLIPPER&gt; #        (see CLIPPER doc.s) and cfortran.h does not fix it up.
CLIPPER&gt; #        [cfortran.h ok for old sun C which made the same mistake.]
CLIPPER&gt; acc cfortest.c -c -DCLIPPERFortran
CLIPPER&gt; af77 cfortex.f cfortest.o -o cfortest
</cftmp.h></font></pre>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -