www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Idea for C++ Interop

reply "Craig Black" <cblack ara.com> writes:
I've been thinking about how to provide interoperability between D and C++.
One idea I had was to compile C++ to C.  I've never used such a compiler but
I'm fairly sure that they exist.

Thoughts?

-Craig
Dec 22 2006
next sibling parent BCS <BCS pathilink.com> writes:
Craig Black wrote:
 I've been thinking about how to provide interoperability between D and C++.
 One idea I had was to compile C++ to C.  I've never used such a compiler but
 I'm fairly sure that they exist.
 
 Thoughts?
 
 -Craig
 

An interesting note, IIRC Walter's C++ compiler was the first that didn't do this.
Dec 22 2006
prev sibling next sibling parent reply "Andrey Khropov" <andkhropov_nosp m_mtu-net.ru> writes:
Craig Black wrote:

 I've been thinking about how to provide interoperability between D and C++.
 One idea I had was to compile C++ to C.  I've never used such a compiler but
 I'm fairly sure that they exist.
 
 Thoughts?

But what benefit can you obtain from this compared to just extern "C" ? The problem is that C++ and D object models (and hence ABIs) aren't completely compatible. So we have to stick to the lowest Common Denominator (C) or use some component model like COM (it's already supported), CORBA, .NET CLR or JVM. -- AKhropov
Dec 23 2006
next sibling parent Paul Findlay <r.lph50+d gmail.com> writes:
 The problem is that C++ and D object models (and hence ABIs) aren't completely
 compatible. So we have to stick to the lowest Common Denominator (C) or use
 some component model like COM (it's already supported), CORBA, .NET CLR or JVM.

(http://gcc.gnu.org/onlinedocs/gcj/About-CNI.html) which supposedly allows one to "write Java native methods using C++." But it has a few restrictions on what types of class member variables you can have, and what classes you can derive from etc just to try and match the object models. Still its kinda cool. As for benefit over extern "C", I for one would love to be using Trolltech's Qt and some of the KDE4 libraries without having to wrap each release. Of course that would be shifting the insanity of dealing with the C++/D mismatch to D compiler writers. - Paul
Dec 23 2006
prev sibling parent "Craig Black" <cblack ara.com> writes:
"Andrey Khropov" <andkhropov_nosp m_mtu-net.ru> wrote in message
news:emireg$krs$1 digitaldaemon.com...
 Craig Black wrote:

 I've been thinking about how to provide interoperability between D and


 One idea I had was to compile C++ to C.  I've never used such a compiler


 I'm fairly sure that they exist.

 Thoughts?

But what benefit can you obtain from this compared to just extern "C" ?

You can't put an extern "C" around a C++ class or template. A C++ to C compiler would automatically convert ALL C++ code to C. Then it could be compiled with DMC, so that you have complete access to all your C++ API via C. The C++ to C conversion might bastardize some of your code, especially the templated stuff. But for some people, it may be worth a try. It would allow you to invoke ALL your C++ code from D, not just the stuff you make extern "C" by hand.
 The problem is that C++ and D object models (and hence ABIs) aren't

 compatible. So we have to stick to the lowest Common Denominator (C) or

 some component model like COM (it's already supported), CORBA, .NET CLR or

Right, C. Yes I know that. That's why I suggested a C++ to C converter. -Craig
Dec 23 2006
prev sibling next sibling parent reply Carlos Santander <csantander619 gmail.com> writes:
Craig Black escribió:
 I've been thinking about how to provide interoperability between D and C++.
 One idea I had was to compile C++ to C.  I've never used such a compiler but
 I'm fairly sure that they exist.
 
 Thoughts?
 
 -Craig
 
 

I posted this on 2004-05-02, so I don't know if any changes have been made since then: ---- OP ---- g++ seems to do a different name mangling than dmc, so this only works on windows. [file: test1.cpp] #include <stdio.h> void foo() { printf("Hi from C++\n"); } [file: test2.d] extern (C++) void foo(); void main() { foo(); }
 dmc -c test1.cpp
 dmd test2.d test1.obj

---- End OP ---- So, if things remain the same, I guess you can compile your project with DMC and have interoperability right away. -- Carlos Santander Bernal
Dec 23 2006
parent "Craig Black" <cblack ara.com> writes:
That's the implication that I was trying to communicate when I said "C++ to
C" compiler.

-Craig

"Carlos Santander" <csantander619 gmail.com> wrote in message
news:emjd06$170f$1 digitaldaemon.com...
 Craig Black escribió:
 I've been thinking about how to provide interoperability between D and


 One idea I had was to compile C++ to C.  I've never used such a compiler


 I'm fairly sure that they exist.

 Thoughts?

 -Craig

I posted this on 2004-05-02, so I don't know if any changes have been made

 then:

 ---- OP ----

 g++ seems to do a different name mangling than dmc, so this only works on
 windows.

 [file: test1.cpp]
 #include <stdio.h>
 void foo() { printf("Hi from C++\n"); }

 [file: test2.d]
 extern (C++) void foo();
 void main() { foo(); }

  > dmc -c test1.cpp
  > dmd test2.d test1.obj

 ---- End OP ----

 So, if things remain the same, I guess you can compile your project with

 have interoperability right away.

 --
 Carlos Santander Bernal

Dec 23 2006
prev sibling parent Leandro Lucarella <llucarella integratech.com.ar> writes:
Craig Black escribió:
 I've been thinking about how to provide interoperability between D and C++.
 One idea I had was to compile C++ to C.  I've never used such a compiler but
 I'm fairly sure that they exist.
 
 Thoughts?

See Lightweight C++: http://students.ceid.upatras.gr/~sxanth/lwc/ -- Leandro Lucarella Integratech S.A. 4571-5252
Dec 26 2006