Linkage to Non-C++ Functions

C functions and data can be accessed only if they are previously declared as having C linkage. However, they must be defined in a separately compiled translation unit.

Syntax

linkage-specification:
externstring-literal{declaration-listopt}
externstring-literal declaration

declaration-list:
declaration
declaration-list declaration

Microsoft C++ supports the strings "C" and "C++" in the string-literal field. The following example shows alternative ways to declare names that have C linkage:

// Declare printf with C linkage.

extern "C" int printf( const char *fmt, ... );

// Cause everything in the header file "cinclude.h"

// to have C linkage.

extern "C"

{

#include <cinclude.h>

}

// Declare the two functions ShowChar and GetChar

// with C linkage.

extern "C"

{

char ShowChar( char ch );

char GetChar( void )

}

// Define the two functions ShowChar and GetChar

// with C linkage.

extern "C" char ShowChar( char ch )

{

putchar( ch );

return ch;

}

extern "C" char GetChar( void )

{

char ch;

ch = getchar()

return ch;

}

// Declare a global variable, errno, with C linkage.

extern "C" int errno;