Calling C functions from C++

S

sumaniitb

We have a third party application coded in C++ which has hook for a
function that I need to implement. Is there a way such that I can
implement it in C and give my code as a library. I cannot use the
'extern C' since the application is built without my header file.

Example:

ThirdParty.cpp

extern void fun2();
fun1(){
....

fun2();

}



My code is supposed to implement fun2() in C.
 
D

dizzy

We have a third party application coded in C++ which has hook for a
function that I need to implement. Is there a way such that I can
implement it in C and give my code as a library. I cannot use the
'extern C' since the application is built without my header file.

Example:

ThirdParty.cpp

extern void fun2();
fun1(){
...

fun2();

}

My code is supposed to implement fun2() in C.

Because when ThirdParty.cpp code is compiled it will see it as a C++
function declaration, it will want to call a C++ function so no, you can't
compile fun2 in C (you can implement it in the C subset of C++ if you wish
but I assume you mean compile it with a C compiler). The only thing I can
think of is to have a translation layer, a fun2 in C++ that will call the
actual fun2 implementation from C code.

Something like:
translation.cpp

extern "C" void fun2_real();

void fun2()
{
fun2_real();
}

ccode.c

void fun2_real() { ... }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top