### Use Linkage Keywords to Specify Calling Conventions

In addition to using the compiler options to specify the calling convention for all of the functions in a program, you can use linkage keywords to specify the calling convention for individual functions.

In C, the linkage of a function or function pointer is given by:

- the attached linkage keyword, if present, else
- the command line option, if used, else
- the default _Optlink linkage.

In C++, the linkage of a nonmember function or function pointer is given by:

- the attached linkage keyword, if present, else
- the command line option, if an extern "C" linkage specification is present, else
- any enclosing C++ linkage specification, if present, else
- C++ linkage.

The linkage of a member function or pointer to member function is:

- the attached linkage keyword, if present, else
- C++ linkage.

Note that linkage specifications do not affect member functions.

The linkage keywords and their equivalent calling convention options are listed in the following table.

Linkage Specification | Operating System | Keyword | Compiler Suboption (if applicable)
extern "SYSTEM" |  | _System | /Ms
extern "OPTLINK" |  | _Optlink | /Mp
extern "C" |  | _System or _Optlink |
extern "C++" |  | _Optlink with mangling, overloading, etc. |
extern "STDCALL" |  | __stdcall | /Mt
extern "CDECL" |  | __cdecl | /Mc
extern "PASCAL" |  | _Pascal |
extern "FAR16CDECL" |  | _Far16 _Cdecl |
extern "FAR16PASCAL" |  | _Far16 _Pascal |
extern "FAR16FASTCALL" |  | _Far16 _Fastcall |
extern "BUILTIN" |  | _BUILTIN |

Note: extern "C++" linkage functions use the _Optlink calling convention, but they also have long internal names (called mangled names) that encode their containing class name and parameter types. In addition, member functions also have an implicit `this` parameter that refers to the object on which they are invoked. This combination of _Optlink rules, name mangling, and the `this` parameter is called C++ linkage, and it is considered distinct from _Optlink alone.

In C programs, you can also use #pragma linkage for all but _stdcall and __cdecl. However, you should use the linkage keywords discussed above rather than the #pragma. Problems encountered when using #pragma linkage include:

- it does not exist in C++, so it can't be used in any headers that will be used in C and C++.
- it is difficult to mark function pointers with #pragma linkage, especially if they are in structures or hidden behind arrays.
- code which uses #pragma linkage tends to be less clear than code using the other methods described earlier.

The following examples illustrate the drawbacks.

    /* Defining a callback function using #pragma linkage */
     typedef int foo(void);
    #pragma linkage(foo, optlink)
    struct ss {
      int x;
      foo * callback;
    };

Clarity is improved when the same code is written using keywords:

     /* Defining a callback function using keywords */
    struct ss {
      int x;
      int (_Optlink * callback)(void);
    };

Furthermore, there are cases where necessary conversions do not happen when #pragma linkage is used. Consider the following:

     void func16(char *);           /*Parameter needs to be converted */
    func() {
      func16(pointer_variable);      /* Conversion does not happen... */
    }
    #pragma linkage (func16, far16 pascal) /* ... because #pragma linkage
                                               occurs after the call */

Here is the equivalent code using keywords:

     void _Far16 _pascal func16(char *);
    func() {
      func16(pointer_variable):     /* Conversion does happen properly */
    }

Demangling Compiled C++ Names with CPPFILT

Summary of Compiler Options
Keywords in C and C++