[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Call C++ from C

flashdog

11/14/2008 6:19:00 AM

Hello,
how can I call this C++ Code from C? I must use C, because Ruby-ffi
understand only 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]

[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;
}
[/code]

Best regards
6 Answers

Ian Collins

11/14/2008 7:11:00 AM

0

flashdog wrote:
> Hello,
> how can I call this C++ Code from C? I must use C, because Ruby-ffi
> understand only C.
>
Provide an extern "C" linkage wrapper.

--
Ian Collins

flashdog

11/14/2008 9:28:00 AM

0

Exist maybe a tool that creat such wrapper?

Ian Collins wrote:
> flashdog wrote:
> > Hello,
> > how can I call this C++ Code from C? I must use C, because Ruby-ffi
> > understand only C.
> >
> Provide an extern "C" linkage wrapper.
>
> --
> Ian Collins

Ian Collins

11/14/2008 10:00:00 AM

0

flashdog wrote:

Please don't top-post.

> Ian Collins wrote:
>> flashdog wrote:
>>> Hello,
>>> how can I call this C++ Code from C? I must use C, because Ruby-ffi
>>> understand only C.
>>>
>> Provide an extern "C" linkage wrapper.
>>
> Exist maybe a tool that creat such wrapper?

Your editor? Only you know what and how you want to call from C.

--
Ian Collins

Zeppe

11/14/2008 10:01:00 AM

0

flashdog wrote:
> Exist maybe a tool that creat such wrapper?
>
please, do not top-post.

The thing is, there is no one-to-one correspondence between C and C++,
and in particular you cannot just automatically expose all your C++ code
as a C interface. I'm afraid the best solution is to think to a
reasonable C interface to your C++ code. That means, you will have a set
of "c-style" functions, with C linkage, that will internally use your
C++ code.

Best wishes,

Zeppe

sean_in_raleigh

11/14/2008 2:56:00 PM

0

On Nov 14, 4:28 am, flashdog <flash...@gmx.net> wrote:
> Exist maybe a tool that creat such wrapper?


Check out SWIG: http://www...

Sean

Default User

11/14/2008 7:11:00 PM

0

flashdog wrote:

> Hello,
> how can I call this C++ Code from C? I must use C, because Ruby-ffi
> understand only C.

<http://www.parashift.com/c++-faq-lite/mixing-c-and-cp...




Brian