C++ to C binding
I'm new to C++ and this is my very first attempt to write C++ to C binding.
Problem: with the code below, i can only create single instance of the set in C. How can I make the variable to create with a new reference address (and keep the other instances) every time i call the function? I had try malloc and memcpy but it doesn't work as C structure does. I suppose it is because of how C++ link the virtual functions. Also returning the local variable doesn't work either (garbage collected?).
Original Definition in C++:
my c wrapper code:
typedef std::set<int> int_set;
typedef struct int_set
extern "C" void * INT_SET_NEW()
static int_set iset;
Never, repeat NEVER create a C++ object using malloc(), and NEVER attempt to copy it around using memcpy(). Unless you relish the prospect of undefined and unpredictable behaviours.
The point is the implementation of this function has to be compiled with a C++ compiler, otherwise it cannot access C++ objects or types. You can't implement it using C code only (unless you use techniques that are specific to your particular compiler which, practically, is rather pointless).
extern "C" void *INT_SET_NEW()
return new int_set();
Practically, it needs to be accompanied by a header file, which can be #include'd in a C source file and compiled with a C compiler, so the function can be called from C code. That header will generally declare the function but not implement it. That header also can not declare any C++ specific types or variables or use C++ specific headers - if it does, it will be impossible to compile it as C.
I'm confused -- the idea is to write a C wrapper around existing C++ code? But as grumpy stated, you would have to compile it using C++ compiler. Could someone explain when this would be useful? The only thing I can think of is if you are writing a project in C, and need a dynamically linked library with an interface in C to use existing C++ code. Is this true? I don't have any experience with legacy C/C++ code.
Yes... I figured that out. That was the reason i said i tried but all attempts were failed.
Originally Posted by grumpy
I had studied a few open source wrapper/binding. I knew the header part since it is common for many C++ to C binding.
I was compiling my code as a static library with a C++ compiled and exposed and linked it with my existing C code via a C compatible header. as what i read for some web sites and some personal experiences usually compiled C/C++ is usually identical in byte code form.
As I learned in C.... the pointer in C is very powerful and flexible... it can points to virtually anything and type cast into virtually any types... This is the best way to address variable type compatibility issues in the binding. if we don't need to expose the class for public access we can just use a dummy structure.
I had some projects written in C and I want to use a library that was written C++. I had no choice but to write a wrapper for it. Otherwise i would have to spend a lot of time to recreate it under C ( actually I did tried to reinvent it using C, but figured out it needs a lot of time to be done) and I'm still learning C++ by trial and error.
Originally Posted by neandrake
edits: oh yes... you can compile it as dynamic library too besides static lib.