Calling C++ code from C
-
Hi, I have the following scenario:
// .cpp implementation
class CFoo
{
public:
int Foo();static CFoo \* CreateFoo() { return new CFoo; }
};
// .c
void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)
Flirt harder, I'm a coder.
mlog || Agile Programming | doxygen -
Hi, I have the following scenario:
// .cpp implementation
class CFoo
{
public:
int Foo();static CFoo \* CreateFoo() { return new CFoo; }
};
// .c
void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)
Flirt harder, I'm a coder.
mlog || Agile Programming | doxygenwhat kinda of error are you getting. My God is more powerfull Than Your God. (the line that divides the world)
-
Hi, I have the following scenario:
// .cpp implementation
class CFoo
{
public:
int Foo();static CFoo \* CreateFoo() { return new CFoo; }
};
// .c
void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)
Flirt harder, I'm a coder.
mlog || Agile Programming | doxygenDoes C has classes? Sonork 100.41263:Anthony_Yio
-
Does C has classes? Sonork 100.41263:Anthony_Yio
No, so this will not compile. You have to put the code of the function that uses the class inside the .cpp file. John
-
what kinda of error are you getting. My God is more powerfull Than Your God. (the line that divides the world)
Sorry for the late reply - I got it working. Error was function not found at linkage. Found out that in a .cpp file, you can call CPP stuff even in an funciton that is declared extern "C" So things look like this:
// .h -------
// C++ declarations
#ifdef __cplusplus
class CFoo { ... };#endif
// C Declarations
#ifdef __cplusplus
extern "C" {
#endif
void * FooCreate();
int FoFoo(void *);
#ifdef __cplusplus
} // extern "C"
#endif// ---- .cpp ----------
CFoo * CFoo::CreateInstance() { ... };
int CFoo::Foo() { ... }extern "C" void * FooCreate() { return (void *) CFoo::CreateInstance(); }
extern "C" int FooFoo(void * f) { return ((CFoo *)f)->Foo(); }Cool! I'm not sure if this is a VC6 only thing - but that shouldn't hurt me much
Flirt harder, I'm a coder.
mlog || Agile Programming | doxygen -
Hi, I have the following scenario:
// .cpp implementation
class CFoo
{
public:
int Foo();static CFoo \* CreateFoo() { return new CFoo; }
};
// .c
void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)
Flirt harder, I'm a coder.
mlog || Agile Programming | doxygendeclaring extern C or not will only tells the compiler how to mangle the function the c++ way or the c way... nothing changes in its functionality.(yeah linking should be carefully done) My God is more powerfull Than Your God. (the line that divides the world)
-
Hi, I have the following scenario:
// .cpp implementation
class CFoo
{
public:
int Foo();static CFoo \* CreateFoo() { return new CFoo; }
};
// .c
void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)
Flirt harder, I'm a coder.
mlog || Agile Programming | doxygenThis will help you in accessing the static member function of C++ in C. //Test1.cpp #include typedef int (* pFunc) (int,int); class D { public : static int Calculate(int iNum1,int iNum2); };` extern "C" { int D::Calculate(int iNum1, int iNum2) { return iNum1 + iNum2; } pFunc p = D::Calculate; } // Test.c #include typedef int (* pFunc) (int,int); extern pFunc p; int main() { int iResult = (*p)(10,20); printf("Value is %d",iResult); return 0; } Hope this will help you! Regards Dinesh