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


Next: How is the main() function declared?




Post Comment


Members Please Login

Name:


Email:
 
(Optional. Used for Notification)

Title:

 
Comment:


Validation Code:
 <=>  (Enter this code in text box)
Subscribe





Daily Email Updates

Get Latest Knowledge Base Updates delivered directly to your Inbox...

Enter your email address:

Latest Knowledge Base Updates
 

C Tutorials

 

Related Tutorials