Thread: C++ to C binding

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    51

    Question 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++:
    Code:
    typedef std::set<int> int_set;
    my c wrapper code:
    Code:
    //dummy structure
    typedef struct int_set
    int_set;
    
    extern "C" void * INT_SET_NEW()
    {
    	static int_set iset;
    
    	return &iset;
    }
    Last edited by megablue; 05-17-2009 at 09:50 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Code:
    extern "C"  void *INT_SET_NEW()
    {
         return new int_set();
    }
    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).

    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    Quote Originally Posted by grumpy View Post
    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.
    Code:
    extern "C"  void *INT_SET_NEW()
    {
         return new int_set();
    }
    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).

    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.
    Yes... I figured that out. That was the reason i said i tried but all attempts were failed.
    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.

    Quote Originally Posted by neandrake View Post
    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.
    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.

    edits: oh yes... you can compile it as dynamic library too besides static lib.
    Last edited by megablue; 05-19-2009 at 08:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boost::shared_ptr and dynamic binding
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 03:50 PM
  2. inet_aton()... Noob needs help with sockets :|
    By Maz in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2005, 04:33 PM
  3. dynamic/static binding
    By faze in forum C++ Programming
    Replies: 7
    Last Post: 07-10-2005, 12:26 PM
  4. dynamic binding
    By freethenet in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-26-2004, 03:31 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM