Last update March 7, 2012

Dwith Swig



ToDo:Most of this page is obsolete since SWIG officially includes support for D now. NG announcement Blog post

Table of contents of this page
Introduction   
Modification to wrap C++ classes   
The changes so far   
Problem   
Implementation   
Wrapped classes   
Plans (To Do List)   
/Examples   
/Messages   
Discussion   
Uploads   
End   
Read this - important update   
New release   
Dsource Project   

Introduction    

SWIG is a program for automatic generation of interfaces to enable other languages to access code written in C or C++. There is a version contributed by Andy Friesen which has a module within it to generate an interface between D and C or C++. SWIG will run under Windows and also under Linux. There are considerable advantages to doing things in this way. The existing headers from either C or C++ can be called from an interface file, which defines the operations to be made available in the target language. A good deal of this is common to interfaces for different target languages, although it is easy to accomodate differences if necessary. The SWIG program uses the interface file as data to generate an interface program automatically, so that if there is a change in the interface it does not have to be recoded. In the cases of D, SWIG generates class interfaces in D which can be called from the application written in D, and which in turn call an interface code which has C linkage, even if the underlying program is in C++. In this way the D program does not need to access any code written in C++, and therefore does not need to use any C++ namemangling directly.

/MoreAboutSwig

Modification to wrap C++ classes    

I have been modifying the distributed version in order to improve the performance and above all avoid any need to make manual alterations to the code generated by SWIG. Note that the code generator for D was adapted from code originally written for Java and then adapted for C#. The code generated includes a separate file in D for each class which has been interfaced. One problem I encountered was the fact that the classes wrapped often referred to each other and in that case some means of importing the definition of one into another is needed

The changes so far    

  • Remove some places where type "char*" was replaced by "string". I have retained "char*".
  • Add a means to put extra lines "import Classname;" into the D files for individual classes.
  • I have also solved the resulting problem of the name of the class file (see /Problem ).
  • Add a means of dealing with the things which SWIG does not understand e.g. pointers.
  • Added support for C++ functions not in a class.
Details of /Modifications . See also /Examples/Function /Examples/Class .

Problem    

The above is not sufficient. There are some things which work when two classes are in the same file, but not if they are in separate files, as in the case of these generated by SWIG.

Details of /Problem and its solution. See also /Examples/Pragma

Implementation    

There needs to be a discussion of the different possibilities of the use of SWIG for D under /Linux and /Windows

Wrapped classes    

qd is a class for extended precision calculations (double double and quadruple double) to be found at the web site of Yozo Hida.

GiNaC is a C++ library for algebra. This is one of the reasons I latched onto SWIG for D.

Plans (To Do List)    

  • I am planning to test these modifications and then pass them back to Andy Friesen for inclusion in his distribution. Alternatively, I could distribute my modifications to his distribution (2 files) plus some examples separately.
  • I also have some plans to wrap some more classes, e.g. for symbolic algebra.
  • I want to look at wrapping STL objects such as string, vector and map which I guess will relate to the DTL I have seen mentioned. I have used these in my use of SWIG for Ruby, where there is a good interface.
  • I regard this work as only partially complete. Trying out some more examples may throw up the need for more changes to the D interface in SWIG.
  • I would like to use stream output from objects, which I have not yet attempted.
  • SWIG has moved on. This work has used version 1.3.19 and it is up to version 1.3.21.

/Examples    

/Messages    

mainly about wxWidgets

Discussion    

I think that for wrapping C this is not needed, as interfaces can be declared directly. I hope it will be helpful in allowing the interfacing of C++ classes, so making things available in D which would otherwise have to be rewritten.

Uploads    

There still needs to be sorted out a way for me to make available my modifications to Andy Friesen's distribution. In the meantime I have uploaded a file swigdmd.zip to the upload page of this wiki. This contains the three files which I have modified from Andy Friesen's distribution. They need to be put into the correct directories, replacing files of the same name.

  • dmd.cxx Source/Modules
  • dmd.swg Lib/dmd
  • dmdhead.swg Lib/dmd
After that, SWIG must be recompiled and installed.

Note: I have only used this under Linux and Cygwin.

The file is available for download by clicking on this link: Upload:duser/swigdmd.zip

I will also add code for the examples, which can be cut and pasted from the wiki.

End    

This completes the examples I have at the moment. I expect to do some more.

Read this - important update    

August 2004 Andy Friesen is working on an improved version. When he has released a new version I will update the examples to work with the new version. In the meantime, these examples only work with the old version plus my modified files.

JohnFletcher

New release    

This is an almost complete rewrite of the previous D backend. Where the previous version was based on the pre-existing C# backend, this one is written from scratch.

You can get it from here: Upload:duser/swig-d-1-Aug-2004.zip

(posted here and not on my HomePage due to maintenance on that system)

This is basically a partial copy of the official SWIG 1.3.21 source distribution. Examples for non-D languages, and a few other things were left out. (if you really want those, you can go to http://www.swig.org and get them from the official maintainers)

Windows binary is included. To build on *nix, all you should really need to do is move the new source trees into the official source package and adjust the autotools scripts to incorporate the extra d.cxx file. (also, don't forget to overwrite or merge Modules/swigmain.cxx, as it was changed a little to add the extra compiler switch)

This is still pretty rough, and I'm sure it needs a lot of work, but it produces flawless output for the "class" example, so it's a fair start, I think.

 -- AndyFriesen

Dsource Project    

Andy also set up a project at dsource, but even that hasn't been active recently.


FolderProjects


FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: March 7, 2012 20:26 (diff))