Simple executables that can be built on the command line and don't use library routines
don't need to worry about naming and linkage conventions, so if that is all you're
ever planning to do, you can skip to the next section.
A programmer who uses both C and Fortran routines has to worry about interlanguage
communication issues, such as whether arguments are passed as values on the stack
or as addresses of other memory locations which contain the values (by reference).
This issue is not any different under Windows than under any other
operating system. However, be aware that calls to routines in different languages
are generally incompatible by default. Detailed information is provided in the module
on
Libraries .
If an executable uses libraries other than the ones
that are provided by the compiler, there are issues that need to be addressed before
you begin compiling.
In the Windows world, there are three basic linkage conventions:
- C declaration [cdecl]
- standard call [stdcall]
- fastcall
These conventions dictate how a compiler places function/subroutine arguments on
the stack and where it expects to find arguments. It
also determines who will remove the arguments from the stack when the call is completed.
If a routine compiled with one convention is called by another that has used a different
convention, the argument values seen by the called routine are usually wrong and
there is a high likelihood that when the routine returns, the caller's stack pointer
will not be where it should be. When that happens, it usually isn't long before
a protection exception occurs.
The linkage convention is usually determined by a compiler option, though it can
be explicitly stated in the source code. Since routines often call and are called
by multiple other routines, it is difficult to build an executable from routines
that have been compiled with different linkage conventions. The standard libraries
that come with the compilers have object files that have been compiled with each
of the calling conventions, so that is not a limitation, but if you are using other
libraries that either you or a third party have created, the conventions used in
creating them may limit your choices for the rest of your project. The fastcall
convention isn't widely used, so we won't discuss it further.
Routines that have been compiled with stdcall present "decorated" names to
the linker. The "decoration" consists of an "@" followed by a number indicating
how many bytes of arguments will be passed. As a general rule, you can determine
the convention used by the routines in a library by using the dumpbin command
that comes with Microsoft Visual Studio. For a library named "mylib.lib, you would
type: dumpbin /linkermember:2 mylib.lib and see a list of entrypoints. If they have
decorations, they use the stdcall convention. If they don't, they use
cdecl. Using this output, you can also determine what case the
names have and whether they have trailing underscores. Compilers have switches that
can be used to change the case of the name that appears in the code to a different
one for references to external routines.
Default behavior of common compilers:
| Compiler |
Linkage convention |
Case of external names |
| Visual C/C++ |
cdecl |
Mixed |
| Intel C/C++ |
cdecl |
Mixed |
| Intel Fortran |
cdecl |
UPPER |