Thursday, November 22, 2012

Accessing C++ codes from C codes

C and C++ are close cousins but I have never tried mixing both codes in one project before. I encountered such a need to mix codes just recently due to work. It is quite difficult to scour the net for reference. And after long hours of experimentation I was able to access C++ codes in my C codes.
Here are the steps:



C main file:
Call the C++ interface

void the_cpp_interface();
C++ header file:
Define wrapper and class:
#ifdef __cplusplus 
extern “C” { void the_cpp_interface()};
 #endif

class cppClass
{
 Public:
                Void cppTestFunc();
                cppClass() {};
}
               
C++ source file:
extern “C” void the_cpp_interface()
{
                //your c++ codes here
}

//class defn
myClass::cppTestFunc()
{
             // codes here
}

0 comments:

Post a Comment