The C programming language is the foundation for C++. One of the main advantages of C++ is that libraries written in C can also be easily used.
External names, however, are handled differently in C and C++ implementations. In contrast to C, external names in C++ are coded so they can be used by BINDER.
To enable the common use of functions and data in C++ and C (or other languages), the coding of external names must therefore be suppressed. The appropriate language element in C++ for this purpose is as follows:
extern "C"
declaration;
or
extern "C" {
declarations
}
The above extern "C"
declarations are required in the C++ program for all functions and data that are defined in C++ as well as those which are defined in C (or other languages).
When standard C library functions are called from C++ source programs, only the corresponding standard header elements (e.g. stdio.h) need to be specified, since they already contain the extern "C"
declarations for all C library functions.
To provide linkage between C and C++, only C89 or C11 objects should be used.
Linkage between C++ objects and K&R C objects is also technically possible, but may create problems in some cases (different restrictions apply to function declarations; float
parameters are passed differently, etc.).