Calling Conventions

Calling conventions used by IBM C and C++ Compilers for C and C++:

OS/2 Calling Conventions Windows Calling Conventions
_Optlink _Optlink
_System _System
__cdecl __stdcall
__stdcall __cdecl
_Pascal and _Far32_Pascal  
16-bit conventions:  
  • _Far16 _Cdecl
  • _Far16 _Pascal
  • _Far16 _Fastcall
 

The _Optlink convention is specific to IBM C and C++ Compilers compiler and is the fastest method of calling C or C++ functions or assembler routines, but it is not standard for all OS/2 and Windows applications.

The _System calling convention, while slower, is standard for all OS/2 applications and is used for calling OS/2 APIs.
There is no _System calling convention, per se. Specifying _System linkage is synonymous with specifying __stdcall. _System linkage will be implemented the same as __stdcall, but be careful to consider that the compiler still considers the names to be distinct, and they should not be mixed. For example:

	void _System f(void);
	void (__stdcall *fp) (void) = f; 	/*error*/

The _Pascal and _Far32 _Pascal conventions are used to develop virtual device drivers. The _Far32 _Pascal convention can only be used for applications written in C that run at ring zero (compiled with the /Gr+ compiler option).

You can specify a default calling convention for all functions within a program using the /Mp, /Ms, /Mc or /Mt compiler options, or by using linkage keywords.

In a C program, the default applies to all functions that do not have a linkage keyword or #pragma linkage applied to them. In a C++ program, the default applies to all non-member functions declared as extern "C". If you do not specify a default, _Optlink is assumed.

Notes:

  1. You cannot call a function using a different calling convention than the one with which it is compiled. For example, if a function is compiled with _System linkage, you cannot later call it specifying _Optlink linkage.
  2. IBM C and C++ Compilers does not allow functions that use the __stdcall calling convention to have both the following characteristics:

    In particular, an unprototyped function that accepts a variable number of arguments and uses the __stdcall calling convention will not link. This is because __stdcall functions are referenced in .OBJ files using a name that is a combination of the function name and the amount of stack space used by the parameters the function expects. Therefore, if one compilation has defined it with a different amount of parameter storage than another compilation unit, the two references to the function will have different external function names. The linker will not be able to resolve them.

    Prototyped functions do not have this problem because the external name is generated based on the prototype, which must be consistent across all compilations.



16-Bit Calling Conventions
Eyecatchers


Use Linkage Keywords to Specify Calling Conventions


__cdecl Calling Convention in Windows
__cdecl Calling Convention in OS/2
_Optlink Calling Convention
_Pascal and _Far32_Pascal Calling Conventions
__stdcall Calling Convention
_System Calling Convention in OS/2
_System Calling Convention in Windows
Calling Conventions for Subsystem Functions