Name Mangling - C Name Decoration in Microsoft Windows

C Name Decoration in Microsoft Windows

Although name mangling is not generally required or used by languages that do not support function overloading (such as C and classic Pascal), they use it in some cases to provide additional information about a function. For example, compilers targeted at Microsoft Windows platforms support a variety of calling conventions, which determine the manner in which parameters are sent to subroutines and results returned. Because the different calling conventions are not compatible with one another, compilers mangle symbols with codes detailing which convention should be used.

The mangling scheme was established by Microsoft, and has been informally followed by other compilers including Digital Mars, Borland, and GNU GCC, when compiling code for the Windows platforms. The scheme even applies to other languages, such as Pascal, D, Delphi, Fortran, and C#. This allows subroutines written in those languages to call, or be called by, existing Windows libraries using a calling convention different from their default.

When compiling the following C examples:

int _cdecl f (int x) { return 0; } int _stdcall g (int y) { return 0; } int _fastcall h (int z) { return 0; }

32 bit compilers emit, respectively:

_f _g@4 @h@4

In the stdcall and fastcall mangling schemes, the function is encoded as _name@X and @name@X respectively, where X is the number of bytes, in decimal, of the argument(s) in the parameter list (including those passed in registers, for fastcall). In the case of cdecl, the function name is merely prefixed by an underscore.

Note that the 64-bit convention on Windows (Microsoft C) is no leading underscore. This difference may in some rare cases lead to unresolved externals when porting such code to 64 bits. For example, Fortran code can use 'alias' to link against a C method by name as follows:

SUBROUTINE f !DEC$ ATTRIBUTES C, ALIAS:'_f' :: f END SUBROUTINE

This will compile and link fine under 32 bits, but generate an unresolved external '_f' under 64 bits. One work around for this is to not use 'alias' at all (in which the method names typically need to be capitalized in C and Fortran), or to use the BIND option:

SUBROUTINE f BIND(C,NAME="f") END SUBROUTINE

Read more about this topic:  Name Mangling

Famous quotes containing the words decoration and/or windows:

    The question mark is alright when it is all alone when it
    is used as a brand on cattle or when it could be used
    in decoration but connected with writing it is
    completely entirely completely uninteresting.... A
    question is a question, anybody can know that a
    question is a question and so why add to it the
    question mark when it is already there when the
    question is already there in the writing.
    Gertrude Stein (1874–1946)

    In winter we lead a more inward life. Our hearts are warm and cheery, like cottages under drifts, whose windows and doors are half concealed, but from whose chimneys the smoke cheerfully ascends.... We enjoy now, not an Oriental, but a Boreal leisure, around warm stoves and fireplaces, and watch the shadow of motes in the sunbeams.
    Henry David Thoreau (1817–1862)