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:
- 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 LF95 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.
LF95 code that calls or is called by another language makes the name available
by giving it the DLL_IMPORT, DLL_EXPORT
or ML_EXTERNAL attribute. The DLL_IMPORT
attribute is used when calling a procedure from a DLL. The
DLL_EXPORT attribute is used to make a procedure name
externally available when creating a Fortran DLL. The
ML_EXTERNAL
attribute is used to make a procedure from another language available to Fortran
or making a Fortran procedure available to be called from another language when
static linking. At compilation time, any procedure names having one of these
attributes are `decorated' to match the calling convention specified by the
-ML option.
- 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. The arguments passing problem is addressed for each supported
language system, described in subsequent sections.