Lahey/GNU Fortran

Mixed Language Overview

Mixed language programming is the process of melding code created by different programming languages into an executable program. There are two possible ways that this might be accomplished: by creating object files with different compilers that are linked into a single executable (static linking); or by creating a dynamic link library with one language, and calling procedures from the library using the other language (dynamic linking). Static linking mixes the different language parts into a single executable program which is self contained. Dynamic linking keeps the different language parts separate, and results in two separate entities, a DLL created with one language, and an executable created with the other language.

Regardless of the method chosen to create a mixed language application, two basic problems need to be overcome by the programmer in order to be successful:

  1. The first problem involves how each language system names its procedures, and how names from one language system can be recognized by the other language system. Each procedure needs to know how the other is named, so that each can call and be called by the other within the execution environment. If the translation between the different naming conventions is not properly done, the programmer will not be able to link the different program parts together, because linker errors concerning unresolved symbols will occur. Resolving the naming problem involves declaring any Fortran procedure names that are to be called from another language, declaring the other language procedure names that will be called in Fortran, and telling LGF what calling convention is being used at compile time with the -ml compiler option. If a DLL is being used, a "translation" between the exported DLL procedures and how Fortran declares the procedures is provided in the form of an import library.

    LGF code that calls or is called by another language makes the name available by giving it the !GCC$ ATTRIBUTES dllimport or !GCC$ ATTRIBUTES dllexport directive. The !GCC$ ATTRIBUTES dllimport directive is used when calling a procedure from a DLL. The !GCC$ ATTRIBUTES dllexport directive is used to make a procedure name externally available when creating a Fortran DLL.

    For more information, see GNU Fortran compiler directives.

  2. Secondly, in order to be useful, the procedures need to be able to pass information back and forth in a way that both can understand and utilize. This involves the passing of arguments to a subroutine or function, passing a function result between language systems, and how basic data types are interpreted by each language system. If arguments are not passed or interpreted correctly, the result can be unpredictable, and can range from nonsense answers to the program crashing with an "illegal operation" message.

    For more information, see Interoperability with C.