FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Call C++ from D

 
Post new topic   Reply to topic     Forum Index -> Build
View previous topic :: View next topic  
Author Message
flashdog



Joined: 10 Oct 2008
Posts: 15

PostPosted: Thu Nov 13, 2008 6:26 am    Post subject: Call C++ from D Reply with quote

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

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
Back to top
View user's profile Send private message
deadimp



Joined: 22 Oct 2008
Posts: 28

PostPosted: Thu Nov 13, 2008 1:14 pm    Post subject: Reply with quote

You can either look into some of the C++ binding utilities there are on DSource, or generate C bindings (using either a tool or just on your own).
_________________
deadimp.org
> MegaMan X Crossfire - MegaMan X fan game

Beginning D; Basic experience in PHP, C++, Java, C#, MySQL, HTML, CSS, JavaScript, VB
Back to top
View user's profile Send private message
flashdog



Joined: 10 Oct 2008
Posts: 15

PostPosted: Thu Nov 13, 2008 6:08 pm    Post subject: Reply with quote

* ForC++ binding utilities you mean BCD?
* Which tool generate C bindings of a C++ code?
* Exist somewhere a howto how I can create C bindings for C++ Code?
Back to top
View user's profile Send private message
clayasaurus



Joined: 21 May 2004
Posts: 857

PostPosted: Thu Nov 13, 2008 6:25 pm    Post subject: Reply with quote

You have to create a Glue library in C that calls the C++ functions you want. You can then call the C function from D.
Back to top
View user's profile Send private message AIM Address
flashdog



Joined: 10 Oct 2008
Posts: 15

PostPosted: Thu Nov 13, 2008 6:31 pm    Post subject: Reply with quote

But how can I creat a Glue library in C that calls the C++ functions?

Do you know where I can find howto to create a Glue library?
Back to top
View user's profile Send private message
clayasaurus



Joined: 21 May 2004
Posts: 857

PostPosted: Fri Nov 14, 2008 9:47 am    Post subject: Reply with quote

You use a C++ compiler and do things like...

Code:

c_function(...)
{
    // Call C++ code
    C++_code_here();
}
[/code]
Back to top
View user's profile Send private message AIM Address
3-S-E



Joined: 16 Jun 2008
Posts: 54

PostPosted: Mon Apr 06, 2009 12:16 am    Post subject: Reply with quote

Or you just compile your class into a library and convert your .h-file into a D-module, like this:

http://www.digitalmars.com/d/1.0/htomodule.html

For simple .h-files you can use this tool:

http://www.digitalmars.com/d/1.0/htod.html
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Build All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group