www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is it possible to wrap c++ functions to use with D (i.e. the Qt gui)?

reply Kramer <Kramer_member pathlink.com> writes:
Trolltech - the company that produces the Qt gui environ. - will be releasing a
GPL Windows version of their software in Q2 2005:
http://www.trolltech.com/developer/faqs/duallicense.html#q113

I'd like to use this gui but, according to the doc, D does not interface with
c++: http://www.digitalmars.com/d/interface.html

So, is there any way to wrap c++ functions so they can be used in D?

I understand that you can directly access a C function by using the extern (C)
linkage declaration, but there's nothing for c++? :(

-Kramer
Feb 21 2005
next sibling parent reply Joshua Cearley <jtech ezoob.com> writes:
Kramer wrote:

 Trolltech - the company that produces the Qt gui environ. - will be
 releasing a GPL Windows version of their software in Q2 2005:
 http://www.trolltech.com/developer/faqs/duallicense.html#q113
 
 I'd like to use this gui but, according to the doc, D does not interface
 with c++: http://www.digitalmars.com/d/interface.html
 
 So, is there any way to wrap c++ functions so they can be used in D?
 
 I understand that you can directly access a C function by using the extern
 (C) linkage declaration, but there's nothing for c++? :(
 
 -Kramer

This is something I've been looking in to for my D Interface Generator project (using C++ libs in D). AFAIK, to create the interface you need to write a C++ library that wraps around the lib you want to use. This new library cannot be object-oriented, so you program like C (things like CLASS_FUNCTION). Then you can export these functions with C linkage. You can import them back in D, and then write a wrapper around those to get the original class structure back. It sounds like a pain in the butt, but I think its the only way without C++ linkage or some fancy hacking. -JC
Feb 21 2005
parent reply remy.moueza free.fr writes:
This is something I've been looking in to for my D Interface Generator
project (using C++ libs in D). AFAIK, to create the interface you need to
write a C++ library that wraps around the lib you want to use. This new
library cannot be object-oriented, so you program like C (things like
CLASS_FUNCTION). Then you can export these functions with C linkage. You
can import them back in D, and then write a wrapper around those to get the
original class structure back. It sounds like a pain in the butt, but I
think its the only way without C++ linkage or some fancy hacking.

-JC

Indeed, this is the only way, IMO. But it works very well: I've written a generation tool in Python that uses gcc_xml output to generate C/C++ wrapping code, D extern(C) importation and a D class hierarchy that mimic the C++ source. I've first managed to compile and run the example given in the modified swig version for D. Then I've used this tool to wrap the FLTK GUI toolkit. The generated code was somewhat buggy but after a few correction I managed to make it working. Currently I've only written and run a few samples with gdc-0.10 on cygwin with FLTK 1.1.4. As it's also a project for my current Engeener college year, I am mainly working on a report and some documentation for the moment but I hope to make a release soon. I would have like to know if anyone had managed to compile FLTK with dmc or if it is possible to use some bcc or Visual C++ ".lib" files, in order to test it with the original dmd. Remy.
Feb 22 2005
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Is this something you'd be willing to post?

remy.moueza free.fr wrote:
This is something I've been looking in to for my D Interface Generator
project (using C++ libs in D). AFAIK, to create the interface you need to
write a C++ library that wraps around the lib you want to use. This new
library cannot be object-oriented, so you program like C (things like
CLASS_FUNCTION). Then you can export these functions with C linkage. You
can import them back in D, and then write a wrapper around those to get the
original class structure back. It sounds like a pain in the butt, but I
think its the only way without C++ linkage or some fancy hacking.

-JC

Indeed, this is the only way, IMO. But it works very well: I've written a generation tool in Python that uses gcc_xml output to generate C/C++ wrapping code, D extern(C) importation and a D class hierarchy that mimic the C++ source. I've first managed to compile and run the example given in the modified swig version for D. Then I've used this tool to wrap the FLTK GUI toolkit. The generated code was somewhat buggy but after a few correction I managed to make it working. Currently I've only written and run a few samples with gdc-0.10 on cygwin with FLTK 1.1.4. As it's also a project for my current Engeener college year, I am mainly working on a report and some documentation for the moment but I hope to make a release soon. I would have like to know if anyone had managed to compile FLTK with dmc or if it is possible to use some bcc or Visual C++ ".lib" files, in order to test it with the original dmd. Remy.

Feb 25 2005
prev sibling next sibling parent nail <nail_member pathlink.com> writes:
In article <cveho1$1u09$1 digitaldaemon.com>, Kramer says...
Trolltech - the company that produces the Qt gui environ. - will be releasing a
GPL Windows version of their software in Q2 2005:
http://www.trolltech.com/developer/faqs/duallicense.html#q113

I'd like to use this gui but, according to the doc, D does not interface with
c++: http://www.digitalmars.com/d/interface.html

Even if D would easily support C++ wraping, using Qt not became trivial. As I know Qt library obligies to use preprocessor named moc (for expanding constructions ala public signals: protected signals: emit, Q_OBJECT, etc, etc) wich, of course, oriented to use within c++ projects. Victor Nakoryakov nail-mail<at>mail<dot>com
Feb 22 2005
prev sibling parent "Carlos Santander B." <csantander619 gmail.com> writes:
Kramer wrote:
 Trolltech - the company that produces the Qt gui environ. - will be releasing a
 GPL Windows version of their software in Q2 2005:
 http://www.trolltech.com/developer/faqs/duallicense.html#q113
 
 I'd like to use this gui but, according to the doc, D does not interface with
 c++: http://www.digitalmars.com/d/interface.html
 
 So, is there any way to wrap c++ functions so they can be used in D?
 
 I understand that you can directly access a C function by using the extern (C)
 linkage declaration, but there's nothing for c++? :(
 
 -Kramer
 
 

DMD accepts extern(C++), but it's non-portable even using DMD, because it only works on Windows with DMC. Also, it only works with functions. In conclussion, while I don't recommend it, maybe it can be useful in one or another project. _______________________ Carlos Santander Bernal
Feb 22 2005