MIXED
Mixed Language Programming


MIXED is a directory of FORTRAN90 programs which contains some simple examples of mixed language programming. In particular, this directory considers situations in which the main program is written in FORTRAN90.

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 FORTRAN90 is that C passes scalar parameters by value, whereas FORTRAN90 passes all variables by address. Thus, if a C routine wants to call a FORTRAN90 subroutine, it must put the reference symbol & in front of each scalar variable to ensure that FORTRAN90 receives what it's expecting.

On the other hand, if a FORTRAN90 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 FORTRAN90, 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. If the C routine cannot be rewritten, then the only other option is to try to use some of the fancier features added to FORTRAN90.

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 FORTRAN90 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 FORTRAN90 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 FORTRAN90 symbolic names. On the other hand, the FORTRAN90 compiler often has a switch that allows the user to turn off the automatic capitalization and underscore-postfixation, which may accomplish the same goal.

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 FORTRAN90 function of real type. If you do your best to write such a routine, it will actually behave as though it is a FORTRAN90 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 FORTRAN90 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 FORTRAN90 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 FORTRAN90 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 FORTRAN90 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 FORTRAN90 command to load, but appending a switch to add the C mathematical library as well:

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

If you are using C to load, you may need to include the appropriate FORTRAN90 libraries. (Warning: the names of these libraries are nowhere near as standard as the C math library. And the FORTRAN90 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 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 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 illustrates how a FORTRAN77 program can call a C function.

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

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

F90_CALLS_C_AND_MPI, FORTRAN90 programs which illustrate how a FORTRAN90 program can call a C function while executing under the MPI parallel programming environment.

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

F90_CALLS_C++_AND_MPI, FORTRAN90 programs which illustrate how a FORTRAN90 program can call a C++ function while executing under the MPI parallel programming environment.

F90_CALLS_MATLAB, FORTRAN90 programs which issue a call to MATLAB to carry out an auxillary calculation.

Reference:

Source Code:

EX1 shows how a FORTRAN90 main program can call a C routine. In this case, the main program wants to add two integers, reals, and double precision values. Files you may copy include:

EX2 shows how a FORTRAN90 program can call a C++ routine. In order to get this to work, the internal C++ routines have to be declared with the extern "C"{...} property. Then, because I couldn't determine the name of the C++ library files, I decided to use the C++ compiler for the loading, and explicitly include the FORTRAN90 libraries. Then I realized that in that case, the FORTRAN90 main program MUST be called "main". Finally, it worked! Files you may copy include:

Examples and Tests:

EX1_FORT_CC uses the FORT and CC compilers. Files you may copy include:

EX1_FORT_GCC uses the FORT and GCC compilers. Files you may copy include:

EX1_GFORTRAN_GCC uses the GFORTRAN and GCC compilers. Files you may copy include:

EX2_FORT_CXX uses the FORT and CXX compilers. Files you may copy include:

EX2_FORT_G++ uses the FORT and G++ compilers. Files you may copy include:

EX2_GFORTRAN_G++ uses the GFORTRAN and G++ compilers. Files you may copy include:

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


Last revised on 25 June 2008