Thread: Call C++ from C

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    Call C++ from C

    Hello,
    how can I call this C++ Code from C ?

    Code:
    class Shape {
    public:
      Shape() {
        nshapes++;
      }
      virtual ~Shape() {
        nshapes--;
      };
      double  x, y;   
      void    move(double dx, double dy);
      virtual double area(void) = 0;
      virtual double perimeter(void) = 0;
      static  int nshapes;
    };
    
    class Circle : public Shape {
    private:
      double radius;
    public:
      Circle(double r) : radius(r) { };
      virtual double area(void);
      virtual double perimeter(void);
    };
    
    class Square : public Shape {
    private:
      double width;
    public:
      Square(double w) : width(w) { };
      virtual double area(void);
      virtual double perimeter(void);
    };
    Code:
    #include "example.h"
    #define M_PI 3.14159265358979323846
    
    /* Move the shape to a new location */
    void Shape::move(double dx, double dy) {
      x += dx;
      y += dy;
    }
    
    int Shape::nshapes = 0;
    
    double Circle::area(void) {
      return M_PI*radius*radius;
    }
    
    double Circle::perimeter(void) {
      return 2*M_PI*radius;
    }
    
    double Square::area(void) {
      return width*width;
    }
    
    double Square::perimeter(void) {
      return 4*width;
    }
    Best regards

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You're going to need a C++ compiler anyway, so why not just use C++? You don't have to use C++ bits in the other parts of your code.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    Unfortunately Ruby-ffi unsderstand only C and not C++, therefore I try to understand how I can reuse C++ code.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    http://www.parashift.com/c++-faq-lit...c-and-cpp.html
    You could well be stuck then.

    So long as you don't have any issues pointed out by the above, then you can do this to invoke a C++ member function from a C call. It's basically the same method used for passing class instances to threads say.
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    class foo {
        public:
            foo(){a=42;}
            static void cToCPlusPlus(void *p);
            void doit ( void );
        private:
            int a;
    };
    void foo::cToCPlusPlus ( void *p ) {
        foo *magic = reinterpret_cast<foo*>(p);
        magic->doit();
    }
    void foo::doit( void ) {
        cout << "The answer is " << a << endl;
    }
    
    extern "C" {
    // This gets
    // - A pointer to a class member function, declared static
    // - A pointer to an instance of the class
    void myCFunc ( void (*fn)(void*), void *instance ) {
        fn(instance);
    }
    }
    
    int main ( ) {
        foo justMe;
        justMe.doit();
        myCFunc( (void(*)(void*))(foo::cToCPlusPlus), &justMe );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from what Salem correctly describes, you can use simple pointers to describe a C++ object, so you don't NEED to use a class member function for this work:
    Code:
    void foo::cToCPlusPlus ( void *p ) {
        foo *magic = reinterpret_cast<foo*>(p);
        magic->doit();
    }
    You could do it this way:
    Code:
    extern "C" {
    // This gets
    // - A pointer to a class member function, declared static
    // - A pointer to an instance of the class
    void myCFunc ( void *instance ) {
        foo *magic = reinterpret_cast<foo*>(instance);
        magic->doit();
    }
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But the object would have to be created first and the constructor invoked. And you'd probably have to call the destructor manually, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Elysia View Post
    But the object would have to be created first and the constructor invoked. And you'd probably have to call the destructor manually, too.
    That's why you normally have a wrapper file, with functions that wrap each operation you'd do on the object, including a construction function, which will construct the object, cast it to a void* and return it.
    It's far from ideal, but if you need a smallish C++ class from C code, this is good way of doing it.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    That's why you normally have a wrapper file, with functions that wrap each operation you'd do on the object, including a construction function, which will construct the object, cast it to a void* and return it.
    It's far from ideal, but if you need a smallish C++ class from C code, this is good way of doing it.

    QuantumPete
    Yes, we have some rather large third party code in C that needs to interface to our overall C++ OS code, and it maintains the objects through storing them as void pointers for the C code, and then calling out to a wrapper layer that casts the void pointer back to a C++ object and does whatever is necessary to the object. This includes, of course, functions to create and destroy the objects as needed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    @matsp: Do you know where can I find examples of your c wrapper?

    @Salem: how can I compile your code?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How?
    How is use a C++ compiler.
    You're going to have to compile your C++ code with a C++ compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by flaxyz View Post
    @matsp: Do you know where can I find examples of your c wrapper?
    Sure, if you work for a large mobile phone company (or, I suppose any other company that wishes to enter into a license agreement), you can license the OS from us. But unfortunately, it's not available as sample code anywhere.

    However, the principle is as I described above, in C++ code with extern "C", call a C++ function. To store object pointers in C use a void *, and when it's needs to be used again in C++ code, cast the void pointer into an object pointer with reinterpret_cast.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 05:44 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM