MIXED
Mixed Language Programming


MIXED is a directory of FORTRAN77 programs which illustrates some use of mixed language programs. In particular, this directory considers situations in which the main program is written in FORTRAN77.

The world of bilingual programming

Because higher level languages end up as machine code, there is some reason to assume that you can write parts of a program in two different languages; what is not standard is the protocol for dealing with the differences in languages.

For instance, C does not explicitly support complex data types, and the C mathematical library does not include the corresponding arithmetic support. It's easy enough to make something that looks like a complex number in C:

        typedef struct ( float r, i } complex;

        complex alpha;

        alpha.r = 1.0;
        alpha.i = 2.0;
      
but you still can't compute directly with complex numbers in C.

Another commonly noticed difference between C and FORTRAN77 is that C passes scalar parameters by value, whereas FORTRAN77 passes all variables by address. Thus, if a C routine wants to call a FORTRAN77 subroutine, it must put the reference symbol & in front of each scalar variable to ensure that FORTRAN77 receives what it's expecting.

On the other hand, if a FORTRAN77 routine needs to pass a scalar parameter to a C routine, there is no standard way to ensure that a value is passed rather than an address. The only possibility is that some vendors have included the ability to compute the address of a variable in FORTRAN77, sometimes by using the same & operator available in C, but this is by no means a universal option. If the C routine can be rewritten, then another option is to preface such scalar variables with the * operator in the routine declaration, indicating that these variables are being passed into the function by address rather than value.

Yet another issue occurs because of things the compiler does to symbolic names. In order to avoid confusion between names the user defines and names the compiler wants to reserve, it is common in some cases to append an underscore to user-defined names. In particular, a FORTRAN77 compiler is likely to take the name of a common block, function, or subroutine, and first CAPITALIZE it, and then append an underscore. This is fine, as long as the compiler also does the same thing when it encounters a line of code that tries to access the same symbolic quantity.

The C compiler is likely to do no such thing. Thus, if the user has written a FORTRAN77 routine called "fred", it may not be possible for a C routine to reference "fred", but it may be possible for it to make the connection by asking for "FRED_"! Thus, if the C code can be adjusted, then the two languages may be able to work together by having the C code be careful about how it refers to FORTRAN77 symbolic names. On the other hand, the FORTRAN77 compiler often has a switch that allows the user to turn off the automatic capitalization and underscore-postfixation, which may accomplish the same goal.

The GNU G77 compiler, for instance, will suppress the underscore business if you compile with the "-fno-underscoring" option.

For historical reasons, a C function that returns a float actually returns a value that is promoted to a double. This means that it is not possible to write a C routine that "looks like" a FORTRAN77 function of real type. If you do your best to write such a routine, it will actually behave as though it is a FORTRAN77 function of double precision type. This is not a problem if the data is being passed back out through the argument list, rather than through the name of the routine.

In some cases, a FORTRAN77 function with COMPLEX value is equivalent to a C function with an extra first argument pointing to the address of the return value:

        complex function f ( x, y )
      
could be mimicked in C by
        F_ ( temp, x, y )
        struct ( float r, i ) *temp;
      

Similarly, a FORTRAN77 function which returns a CHARACTER value may be equivalent to a C function with two extra initial arguments giving the address and length of the return value:

        character*14 function g ( x, y )
          or
        character ( len = 14 ) function g ( x, y )
      
could be mimicked in C by
        G_ ( char result[], long *length, x, y )
      

Analogously, passing a character variable anywhere in a FORTRAN77 argument list may be equivalent to passing the pair of values consisting of the character string and its length.

Even if you get the two parts of your program to be compatible, you still have to worry about what happens when you load them, because both FORTRAN77 and C provide certain auxiliary I/O and math libraries. If things are going your way, you may be able to get away with using the FORTRAN77 command to load, but appending a switch to add the C mathematical library as well:

        f77 my_main.o my_sub.o -lm
      
or, perhaps there is also a "C" library:
        f77 my_main.o my_sub.o -lc -lm
      

If you are using C to load, you may need to include the appropriate FORTRAN77 libraries. (Warning: the names of these libraries are nowhere near as standard as the C math library. And the FORTRAN77 IO and math libraries may be distinct):

        cc my_main.o my_sub.o -lfor -lm
      

C++ supports (on purpose!) a scheme in which the names chosen by a user for various functions are automatically mangled, because it is assumed likely that the same name could be used in different namespaces, so C++ avoids ambiguity by constructing unique internal names when compiling. Unfortunately, this makes it very difficult for programs written in other languages to interact with a C++ program. One feature that can help is the use of the statement

        external "C" { (list of function declarations) }
      
in a C++ program in which name mangling is to be deactivated. The list of function declarations can be either the names of C routines to be called from this C++ routine, OR the names of internal C++ routines that are to be called by an external C routine.

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Languages:

MIXED language programming examples are available in a C version and a C++ version and a FORTRAN77 version and a FORTRAN90 version

Related Data and Programs:

C_CALLS_F77, C programs which illustrate a C program calling a FORTRAN77 subroutine.

C_CALLS_F90, C programs which illustrate a C program calling a FORTRAN90 subroutine.

C++_CALLS_F77, C++ programs which illustrate how a C++ main program can call a FORTRAN77 subroutine.

C++_CALLS_F90, C++ programs which illustrate how a C++ main program can call a FORTRAN90 subroutine.

F77_CALLS_C, FORTRAN77 programs which illustrate how a FORTRAN77 program can call a C function.

F77_CALLS_C++, FORTRAN77 programs which illustrate how a FORTRAN77 program can call a C++ function.

F90_CALLS_C, FORTRAN90 programs which illustrate how a FORTRAN90 program can call a C function.

F90_CALLS_C++, FORTRAN90 programs which illustrate how a FORTRAN90 program can call a C++ function.

MATLAB_CALLS_F77, MATLAB programs which call a FORTRAN77 function, using MATLAB's MEX facility.

Reference:

Source Code:

EX1 involves a FORTRAN77 main program which calls a C routine. Files you may copy include:

Examples and Tests:

ex1_f77_cc uses the f77 and cc compilers. Files you may copy include:

ex1_g77_gcc uses the g77 and gcc compilers. Files you may copy include:

You can go up one level to the FORTRAN77 source codes.


Last revised on 04 January 2006.