GeekInterview.com
  I am new, Sign me up!
 
Home C
 

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

 
Category: C
Comments (0)


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>
}



Read Next: How is the main() function declared?



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape