tags:

views:

207

answers:

6

I have a header declaring functions that take pointers to C++ objects as parameters. The implementaton is in a seperate C++ file. How can I include this header in C and use the functions in C even though the arguments need to be C++ object pointers?

+4  A: 

Unfortunately, my first attempt answered the wrong question....

For the question you did ask...

You can, as someone point out, pass around void *'s. And that's what I would also recommend. As far as C is concerned, pointers to C++ objects should be totally opaque.

C++ functions can be labeled extern "C" as well if they are in the global namespace. Here is an example:

myfunc.hpp:

#ifdef __cplusplus
extern "C" {
#endif

extern int myfunction(int, void *ob);

#ifdef __cplusplus
}
#endif

myfunc.cpp:

#include "myfunc.hpp"

void myfunction(int x, void *vobptr)
{
    ClassType *ob = static_cast<ClassType *>(vobptr);
}

afoofile.c

#include "myfunc.hpp"

void frobble(int x, void *opaque_classtype_ptr) {
    myfunction(x, opaque_classtype_ptr);
    /* do stuff with buf */
}

The other option is to do basically the same thing with creative use of typedefs in C. This, IMHO, is quite ugly, but here is an example anyway:

myfunc.hpp:

#ifdef __cplusplus
extern "C" {
#else
typedef void ClassType;  /* This is incredibly ugly. */
#endif

extern int myfunction(int, ClassType *ob);

#ifdef __cplusplus
}
#endif

myfunc.cpp:

#include "myfunc.hpp"

void myfunction(int x, ClassType *ob)
{
    // Do stuff with ob
}

afoofile.c

#include "myfunc.hpp"

void frobble(int x, ClassType *opaque_classtype_ptr) {
    myfunction(x, opaque_classtype_ptr);
    /* do stuff with buf */
}
Omnifarious
The more usual idiom for conditionally using `extern "C"` can be found in my answer at http://stackoverflow.com/questions/2941126/c-explicit-dll-loading-first-chance-exception-on-non-extern-c-functions/2941360#2941360 (it won't fit nicely into a comment). It's doesn't involve repeating your prototypes, so it's much less prone to typos.
Ken Bloom
@Ken Bloom - that makes sense, and I'll edit my version to conform, though it makes the stupid trick in the second example less clear.
Omnifarious
@Omnifarious: if it makes the pointer trick less clear, then *don't* use it.
Ken Bloom
A: 

Check out this link:-

http://developers.sun.com/solaris/articles/mixing.html

The link contains the following topics:

  • Using Compatible Compilers

  • Accessing C Code From Within C++ Source

     -  Accessing C++ Code From Within C Source
    
  • Mixing IOstream and C Standard I/O

  • Working with Pointers to Functions

  • Working with C++ Exceptions

  • Linking the Program

wiz kid
1. posting links with no explanation, description or summary is discouraged on SO (it makes your answer useless if the page you linked to goes down, it makes it harder for a reader to get an overview of the various answers, and some people like to know that what they're clicking on isn't just spam, or even that it is relevant to the question). And 2. you didn't even format it as a link.
jalf
@jaif: Sorry!!!
wiz kid
A: 

The secret is "extern C", whose primary purpose is the prevention of name decoration.

Alexander Rafferty
+3  A: 

If your C code just needs to pass the pointers around, and eventually pass it back to some C++ that'll actually deal with the object it points to, you should be able to use a void * in the C code, and cast back to T * when it goes back into C++.

If you plan on the C code actually using the pointer, you're pretty much stuck with reverse engineering what your compiler happens to do, and trying to emulate it closely enough to make things work. Even at best, this is going to be ugly and fragile.

Jerry Coffin
A: 

You can't. You'll have to create a C-compatible abstraction layer:

typedef struct foowrapper *foohandle;

foohandle foo_create();
void foo_delete(foohandle);

int foo_getvalue(foohandle);
void foo_dosomething(foohandle, const char* str);
Marcelo Cantos
A: 

Make a wrapper module that's C++ but whose external functions are declared extern "C". This will allow you to access C++ code cleanly from C. Naturally the wrapper should replace any pointers to types not representable in C (i.e. classes) with either void pointers (the quick and dirty solution) or pointers to incomplete struct types (which would provide some level of type-safety as long as they're used consistently.

R..