tags:

views:

207

answers:

2

Hi all,

is there any way where I can call c++ code from a c code

class a
{
  someFunction();
};

how to call someFunction() from a c code.

in other way I am asking how to avoid name mangling here

regards Vinayaka Karjigi

+2  A: 

How to mix C and C++

kitchen
Specifically http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.8
a_m0d
i have added extern "c" in header file of c++ code, but linker errors are comming.I am compiling it on symbian SDK
Vinayaka Karjigi
+1  A: 
  1. First, write a C API wrapper to your object-based library. For example if you have a class Foo with a method bar(), in C++ you'd call it as Foo.bar(). In making a C interface you'd have to have a (global) function bar that takes a pointer to Foo (ideally in the form of a void pointer for opacity).
  2. Wrap the DECLARATIONS for the C API you've exported in extern "C".

(I don't remember all the damned cast operators for C++ off-hand and am too lazy to look it up, so replace (Foo*) with a more specific cast if you like in the following code.)

// My header file for the C API.
extern "C"
{
  void bar(void* fooInstance);
}

// My source file for the C API.
void bar(void* fooInstance)
{
  Foo* inst = (Foo*) fooInstance;
  inst->bar();
}
JUST MY correct OPINION
Why use void* instead of the actual type? Passing any random type coupled with c-style cast just seems like a recipe for disaster.
Mark B
Because the "actual type" is a C++ construct and has no (safe) C equivalent.
JUST MY correct OPINION