Linkage Specifications - Linking to non-C++ Programs

You can link C++ object modules to object modules produced using other source languages such as C and Fortran by using a linkage specification.

The syntax is:

>>--extern--string-literal----declaration----------------><
                            |    /---------------\    |
                            |                   |    |
                            \-{---------------------}-/
                                  \-declaration-/

The string-literal is used to specify the linkage associated with a particular function. For example:

/**
 ** This example illustrates linkage specifications
 **/

extern "C" int printf(const char*,...);
void main()
{
      printf("hello\n");
}

Here the string-literal, "C", tells the compiler that the routine printf(char*,...) has C linkage. Note that string literals used in linkage specifications are not case sensitive.

Some valid values for string-literal are:


If the value of string-literal is not recognized, C type linkage is used.


Overview of the C++ Language
Overview of the C Language