www.digitalmars.com         C & C++   DMDScript  

D - C/C++ wrapping D

reply Mark Evans <Mark_member pathlink.com> writes:
D has easy ways to call C/C++, but is the other direction as easy?  
What if I want C/C++ to form the outer shell of my app (housing WinMain() or
equivalent) with D as an internal component?

Mark
Sep 17 2002
next sibling parent reply Toyotomi <io219 attbi.com> writes:
On Wed, 18 Sep 2002 06:57:02 +0000 (UTC), Mark Evans
<Mark_member pathlink.com> wrote:

D has easy ways to call C/C++, but is the other direction as easy?  
What if I want C/C++ to form the outer shell of my app (housing WinMain() or
equivalent) with D as an internal component?

Mark

I'd just like to point out that calling C from D is as easy as calling C from Delphi... I mention this because, like Delphi, it means that the C and C++ headers must be converted. Once someone does the conversion (and continually updates it as needed), yes it's certainly easy. Some libraries are going to be insane to convert and keep updated. I've seen the same thing with Delphi, and it has led me to disregard Delphi for some projects completely... Perhaps the first D community project should be a program to help with these conversions, however convoluted and difficult. Every hour spent on such a project could certainly save many more...
Sep 18 2002
parent Mac Reiter <Mac_member pathlink.com> writes:
I realize the original question related to C calling D, but Toyotomi's response
is discussing D calling C, and the need for an automated system for converting C
headers to D importable modules.  It has already been mentioned on the
newsgroup, but has anyone looked into making a SWIG module to do this?  I know
they have them for Python and various other languages.  I ran across it looking
for a way to easily import C stuff into Eiffel, but the SWIG modules for that
were pretty flakey and I wasn't real excited about getting into it all that
deeply.

Since D has many common concepts with C (at least at the function interface
level), the SWIG module for this shouldn't be too horrible to develop.  Anyone
with more experience care to comment?

(Sorry for highjacking the original question (C calling D), which I am also
interested in.)
Mac

In article <9kpgouc8iof5qd6t87jh4peqa82b40ph27 4ax.com>, Toyotomi says...
On Wed, 18 Sep 2002 06:57:02 +0000 (UTC), Mark Evans
<Mark_member pathlink.com> wrote:

D has easy ways to call C/C++, but is the other direction as easy?  
What if I want C/C++ to form the outer shell of my app (housing WinMain() or
equivalent) with D as an internal component?

Mark

I'd just like to point out that calling C from D is as easy as calling C from Delphi... I mention this because, like Delphi, it means that the C and C++ headers must be converted. Once someone does the conversion (and continually updates it as needed), yes it's certainly easy. Some libraries are going to be insane to convert and keep updated. I've seen the same thing with Delphi, and it has led me to disregard Delphi for some projects completely... Perhaps the first D community project should be a program to help with these conversions, however convoluted and difficult. Every hour spent on such a project could certainly save many more...

Sep 18 2002
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Mark Evans" <Mark_member pathlink.com> wrote in message
news:am983u$108o$1 digitaldaemon.com...
 D has easy ways to call C/C++, but is the other direction as easy?
 What if I want C/C++ to form the outer shell of my app (housing WinMain()

 equivalent) with D as an internal component?

It's pretty straightforward, just declare the exported D functions with: extern (C) { ... functions ... } and then call them from your C/C++ code as if they were C functions. The main issue will be just to ensure that the D startup code (in phobos\dmain.d) gets called before any D functions.
Sep 18 2002
parent Mark Evans <Mark_member pathlink.com> writes:
Ah yes; Walter, you are a gift to mankind. <g>

It's pretty straightforward, just declare the exported D functions with:
    extern (C)
    {
        ... functions ...
    }
and then call them from your C/C++ code as if they were C functions.

The main issue will be just to ensure that the D startup code (in
phobos\dmain.d) gets called before any D functions.

Sep 18 2002