Go to the first, previous, next, last section, table of contents.


Other Languages

Note: This portion of the documentation definitely needs a lot of work!

Tools and advice for interoperating with C and C++

The following discussion assumes that you are running g77 in f2c compatibility mode, i.e. not using `-fno-f2c'. It provides some advice about quick and simple techniques for linking Fortran and C (or C++), the most common requirement. For the full story consult the description of code generation. See section Debugging and Interfacing.

When linking Fortran and C, it's usually best to use g77 to do the linking so that the correct libraries are included (including the maths one). If you're linking with C++ you will want to add `-lstdc++', `-lg++' or whatever. If you need to use another driver program (or ld directly), you can find out what linkage options g77 passes by running `g77 -v'.

C Interfacing Tools

Even if you don't actually use it as a compiler, `f2c' from @url{ftp://ftp.netlib.org/f2c/src}, can be a useful tool when you're interfacing (linking) Fortran and C. See section Generating Skeletons and Prototypes with f2c.

To use f2c for this purpose you only need retrieve and build the `src' directory from the distribution, consult the `README' instructions there for machine-specifics, and install the f2c program on your path.

Something else that might be useful is `cfortran.h' from @url{ftp://zebra/desy.de/cfortran}. This is a fairly general tool which can be used to generate interfaces for calling in both directions between Fortran and C. It can be used in f2c mode with g77---consult its documentation for details.

Accessing Type Information in C

Generally, C code written to link with g77 code--calling and/or being called from Fortran--should `#include <f2c.h>' to define the C versions of the Fortran types. Don't assume Fortran INTEGER types correspond to C `int's, for instance; instead, declare them as integer, a type defined by `f2c.h'. `f2c.h' is installed where gcc will find it by default, assuming you use a copy of gcc compatible with g77, probably built at the same time as g77.

Generating Skeletons and Prototypes with f2c

A simple and foolproof way to write g77-callable C routines--e.g. to interface with an existing library--is to write a file (named, for example, `fred.f') of dummy Fortran skeletons comprising just the declaration of the routine(s) and dummy arguments plus `END' statements. Then run f2c on file `fred.f' to produce `fred.c' into which you can edit useful code, confident the calling sequence is correct, at least. (There are some errors otherwise commonly made in generating C interfaces with f2c conventions, such as not using doublereal as the return type of a REAL FUNCTION.)

f2c also can help with calling Fortran from C, using its `-P' option to generate C prototypes appropriate for calling the Fortran.(1) is probably better for this purpose.} If the Fortran code containing any routines to be called from C is in file `joe.f', use the command f2c -P joe.f to generate the file `joe.P' containing prototype information. #include this in the C which has to call the Fortran routines to make sure you get it right.

See section Arrays (DIMENSION), for information on the differences between the way Fortran (including compilers like g77) and C handle arrays.

C++ Considerations

f2c can be used to generate suitable code for compilation with a C++ system using the `-C++' option. The important thing about linking g77-compiled code with C++ is that the prototypes for the g77 routines must specify C linkage to avoid name mangling. So, use an `extern "C"' declaration. f2c's `-C++' option will take care of this when generating skeletons or prototype files as above, and also avoid clashes with C++ reserved words in addition to those in C.

Startup Code

Unlike with some runtime systems, it shouldn't be necessary (unless there are bugs) to use a Fortran main program to ensure the runtime--specifically the i/o system--is initialized. However, to use the g77 intrinsics GETARG() and IARGC() the main() routine from the `libf2c' library must be used, either explicitly or implicitly by using a Fortran main program. This main() program calls MAIN__() (where the names are C-type extern names, i.e. not mangled). You need to provide this nullary procedure as the entry point for your C code if using `libf2c''s main. In some cases it might be necessary to provide a dummy version of this to avoid linkers complaining about failure to resolve MAIN__() if linking against `libf2c' and not using main() from it.


Go to the first, previous, next, last section, table of contents.