Linkage Specifications

This next feature is not so much a C++ extension to C as a way to let the two languages co-exist. A “linkage specification” makes C functions accessible to a C++ program. Because there are differences in the way the two languages work, if you call functions originally compiled in C, you must inform the C++ compiler of that fact.

The following example uses a linkage specification to tell the C++ compiler that the functions in MYLIB.H were compiled by a C compiler.

// Linkage specifications

#include <iostream.h>

extern "C"

{ // The linkage specification

#include "mylib.h" // tells C++ that mylib functions

} // were compiled with C

void main()

{

cout << myfunc();

}

The extern "C" statement says that everything in the scope of the braces is compiled by a C compiler. If you do not use the braces, the linkage specification applies only to the declaration that follows the extern statement on the same line.

You can also put the linkage specification in the header file that contains the prototypes for the C functions. Microsoft C supports both C and C++ and includes the linkage specification in the standard C header files. You don't need to use the extern "C" statement when you're calling standard library functions.

Sometimes, however, you need to use linkage specifications for other C header files. If you have a large library of custom C functions to include in your C++ program, and you do not want to port them to C++, you must use a linkage specification. For example, perhaps you have libraries, but not the original source code.

Occasionally you need to tell the C++ compiler to compile a function with C linkages. You would do this if the function was to be called from another function that was itself compiled with C linkage.

The following example illustrates a function that is to be compiled with C linkage because it is called from a C function.

// Linkage specifications

#include <iostream.h>

#include <stdlib.h>

#include <string.h>

// ------ Prototype for a C function

extern "C" int comp( const void *a, const void *b );

void main()

{

// --------- Array of string pointers to be sorted

static char *brothers[] = {

"Frederick William",

"Joseph Jensen",

"Harry Alan",

"Walter Elsworth",

"Julian Paul"

};

// ---------- Sort the strings in alphabetical order

qsort( brothers, 5, sizeof(char *), comp );

// ---------- Display the brothers in sorted order

for( int i = 0; i < 5; i++ )

cout << '\n' << brothers[i];

}

// ---------- A function compiled with C linkage

extern "C"

{

int comp( const void *a, const void *b )

{

return strcmp( *(char **)a, *(char **)b );

}

}

This program calls the C qsort function to sort an array of character pointers. The qsort function expects you to provide a function that compares two items. But qsort is a C function, so you must provide a C-compatible comp function. Because this program is a C++ program, you must tell the C++ compiler to use C linkage for this function alone. Both the prototype and the function definition have the extern "C" linkage specification.