Difference between revisions of "Cross Compiling C and Fortran"

From assela Pathirana
Jump to navigationJump to search
Line 16: Line 16:


<table border=".5">
<table border=".5">
            <tbody>
 
               <tr  >
               <tr  >
                 <th>FORTRAN</th>
                 <th>FORTRAN</th>
Line 26: Line 26:
                 <td>void foo_(...)<b>_</b>( ... )</td>
                 <td>void foo_(...)<b>_</b>( ... )</td>
               </tr>
               </tr>
            </tbody>
 
           </table>
           </table>

Revision as of 16:54, 1 April 2010

TreeTrunk.jpg

Recently I had to mix two large models written in C and Fortran (95 standard) to build a single monolithic program. In theory cross compilation is easy -- in practice its far from it. Your run off the mill 'hello world' implemented in C and called from Fortran (or vice versa) is one thing, but marrying off two large programs (numerous code files in each) written in two languages is a whole different story. Following is an account of my experience:

Summary
What worked for me 1) Sun Studio 11 works brilliantly. It's only available on Linux & Solaris platforms (not on Windows), but that is what got me though. (Free as in beer) 2) Intel compilers are also good. But, getting them to work effectively is a real pain. Download multiple gigabyte Microsoft Visual Studio 90 day trial from Microsoft (Even if you don't want to use their compilers!), then download C and Fortran compiler trial versions from Intel!! 3) Eclipse CDT/Photran (no that is not a spelling mistake!) is nice. But, you must know your way around configuring eclipse -- its a massive system that includes everything plus the Kitchen Sink. But, the good news is it is Free and Open Source (Free as in speech).

First the Theory

To repeat, cross compiling should simply work -- in theory. But in practice, there are numerous points to get straight before anything works. The following two web sites helped me a great deal for learning the 'rules'.

  1. Using C/C++ and Fortran together
  2. Using C and C++ with Fortran

Most important things I learned:

  1. Fortran compiler and C compiler does name mangling in very different ways. It totally depends on the specific compiler and flags used. One common pattern is
FORTRAN C
CALL FOO( ... ) void foo_(...)_( ... )