Headers can be called by using extern C
Syntax of extern C is:
extern "C" <function declaration>
for example to call C functions from C++ we can write
extern "C" {
<function declaration>
<function declaration>
…
<function declaration>
}
If we want to include a header file sample.h in C++ code it is done by declaring the header file in extern C as follows:
extern "C" {
#include "sample.h"
}
The errors that occur if there is mistake in doing the above would result in linage errors rather than compile time errors.
Say for example instead of declaring as extern C if one declare as extern alone as
extern char *sample()
then it would return referencing error. This is a link error and the reason for its occurrence is C++ compiler encrypts the function name because of its function overloading feature. In order to avoid confusion and errors the declaration must be made as
extern "C" {
<function declaration>
<function declaration>
…
<function declaration>
}