To call a C program in C++ , the basic requirement is both the compilers and runtime libraries must be compatible. They must define the primitive data types such as int, float, char etc.
Accessing C Code From Within C++ Source:
A linkage specification is provided by C++, which is used to declare a function that follows the program linkage conventions for a supported language. C++ compilers support C linkage for compatible C compilers.
To access a function with C linkage, the function need to be declared to have C linkge.
The following code snippet depicts the linkage declaration:
extern "C" {
void f(); // C linkage
extern "C++" {
void g(); // C++ linkage
extern "C" void h(); // C linkage
void g2(); // C++ linkage
}
extern "C++" void k();// C++ linkage
void m(); // C linkage
}
The following code snippet depicts the inclusion of a c header file.
extern "C"
{
#include "header.h"
}