Language linkage (C++ only)

Linkage between C++ and non-C++ code fragments is called language linkage. All function types, function names, and variable names have a language linkage, which by default is C++.

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

Read syntax diagramSkip visual syntax diagramLinkage specification syntax
 
>>-extern--string_literal--+-declaration---------------+-------><
                           |    .-----------------.    |
                           |    V                 |    |
                           '-{----+-------------+-+--}-'
                                  '-declaration-'
 

The string_literal is used to specify the linkage associated with a particular function. String literals used in linkage specifications should be considered as case-sensitive. All platforms support the following values for string_literal:

"C++"
Unless otherwise specified, objects and functions have this default linkage specification.
"C"
Indicates linkage to a C procedure

Calling shared libraries that were written before C++ needed to be taken into account requires the #include directive to be within an extern "C" {} declaration.

extern "C" {
#include "shared.h"
}

The following example shows a C printing function that is called from C++.

//  in C++ program
extern "C" int displayfoo(const char *);
int main() {
    return displayfoo("hello");
}

/*  in C program     */
#include <stdio.h>
extern int displayfoo(const char * str) {
    while (*str) {
       putchar(*str);
       putchar(' ');
       ++str;
    }
    putchar('\n');
} 

Name mangling (C++ only)

Name mangling is the encoding of function and variable names into unique names so that linkers can separate common names in the language. Type names may also be mangled. Name mangling is commonly used to facilitate the overloading feature and visibility within different scopes. The compiler generates function names with an encoding of the types of the function arguments when the module is compiled. If a variable is in a namespace, the name of the namespace is mangled into the variable name so that the same variable name can exist in more than one namespace. The C++ compiler also mangles C variable names to identify the namespace in which the C variable resides.

The scheme for producing a mangled name differs with the object model used to compile the source code: the mangled name of an object of a class compiled using one object model will be different from that of an object of the same class compiled using a different object model. The object model is controlled by compiler option or by pragma.

Name mangling is not desirable when linking C modules with libraries or object files compiled with a C++ compiler. To prevent the C++ compiler from mangling the name of a function, you can apply the extern "C" linkage specifier to the declaration or declarations, as shown in the following example:

extern "C" {
   int f1(int);
   int f2(int);
   int f3(int);
};

This declaration tells the compiler that references to the functions f1, f2, and f3 should not be mangled.

The extern "C" linkage specifier can also be used to prevent mangling of functions that are defined in C++ so that they can be called from C. For example,

extern "C" {
   void p(int){
      /* not mangled */
   }
};

In multiple levels of nested extern declarations, the innermost extern specification prevails.

extern "C" {
      extern "C++" {
            void func();
      }
}

In this example, func has C++ linkage.