11.9 C++ Calls to High-Level Languages

C++ lets you specify a linkage specification to permit communication between a C++ module and modules written in other languages. Microsoft C/C++ supports only the “C” linkage specification.

You declare a linkage specification as follows:

extern “C”

{

void prn();

}

This example declares prn to be a function with C linkage. Calls to that function are made using the C calling convention.

To call functions written in languages other than C, declare the function as you would in C and use a “C” linkage specification. For example, to call the Pascal function fact, declare it as follows:

extern “C” { int __pascal fact( int n ); }

This example declares fact to be a function with the Pascal calling convention.

If you want a C++ function to be called from other languages, you must declare it with the extern “C” linkage specification. Such a function can be called from another language in the same way a C function is called. You cannot declare a member function with a linkage specification. You can specify a linkage specification for only one instance of an overloaded function. All other instances of an overloaded function have C++ linkage.

For more information on the extern “C” linkage specification, see the C++ Language Reference.