How to call C header that is not provided generally by system in C++?

This can be done using extern C


Syntax of extern C is:



extern "C"



That is for calling C functions from C++ we can write as



extern "C" {





...



}



That is it if we want to include a header file say sample.h in C++ code it is done by declaring it 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 say as



extern char *sample()



then it would return error as referencing error. This is a link error and the reason for its occurrence is C++ compiler encrypts the function name because of its feature of function overloading. So in order to avoid all this confusion and errors the declaration must be



extern "C" {





...



}


Questions by GeekAdmin   answers by GeekAdmin

 

This Question is not yet answered!

 
 

Related Answered Questions

 

Related Open Questions