www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D Newbie Trying to Use D with Major C Libraries

reply Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
I am a volunteer developer with the well-known 3D CAD FOSS project BRL-CAD:

  http://brlcad.org

I have wanted to use D for a long time but I hadn't taken the plunge.
Yesterday I advertised to the BRL-CAD community my new project to
attempt to create D bindings for BRL-CAD's C libraries, and I created
a branch for the project.

I have been looking for specific information on creating D bindings
from C headers for which there seems to be sufficient information
available, but I would appreciate recommendations as to the best
method.  I have successfully built my first pure D program but now
need to test the feasibility of my project.

What I have not seen yet is the exact way to build a D program which
uses D bindings and its matching C library.  I have just created a
Cookbook page on the D Wiki where I show my first attempt for a real
GNU Makefile as an example for the project.  The page link is here:

  http://wiki.dlang.org/Using_C_libraries_for_a_D_program

I would appreciate it if an experienced D user would correct that
recipe so it should compile the desired binary source correctly
(assuming no errors in the  input files).

Thanks for any help.

Best regards,

-Tom
May 15 2014
next sibling parent reply "Craig Dillabaugh" <craig.dillabaugh gmail.com> writes:
On Thursday, 15 May 2014 at 22:25:47 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
 I am a volunteer developer with the well-known 3D CAD FOSS 
 project BRL-CAD:

   http://brlcad.org

 I have wanted to use D for a long time but I hadn't taken the 
 plunge.
 Yesterday I advertised to the BRL-CAD community my new project 
 to
 attempt to create D bindings for BRL-CAD's C libraries, and I 
 created
 a branch for the project.

 I have been looking for specific information on creating D 
 bindings
 from C headers for which there seems to be sufficient 
 information
 available, but I would appreciate recommendations as to the best
 method.  I have successfully built my first pure D program but 
 now
 need to test the feasibility of my project.

 What I have not seen yet is the exact way to build a D program 
 which
 uses D bindings and its matching C library.  I have just 
 created a
 Cookbook page on the D Wiki where I show my first attempt for a 
 real
 GNU Makefile as an example for the project.  The page link is 
 here:

   http://wiki.dlang.org/Using_C_libraries_for_a_D_program

 I would appreciate it if an experienced D user would correct 
 that
 recipe so it should compile the desired binary source correctly
 (assuming no errors in the  input files).

 Thanks for any help.

 Best regards,

 -Tom
Hi Tom, Sadly, I lack the expertise to give you much advice. I did read through your Wiki posting though. One thing that came to mind was you used GMake. Perhaps you should consider using DUB. For example here is the DUB config file for one of my library bindings (in my case I used a static library though): { "name": "shplib", "description": "D bindings for Shapelib. Shapefile reader.", "homepage": "https://github.com/craig-dillabaugh/shplib.d", "homepage": "http://shapelib.maptools.org/", "importPaths":["."], "targetType": "sourceLibrary", "authors": [ "Craig Dillabaugh" ], "sourcePaths": ["./source"], "libs-posix" : ["libshp"] } A little nicer that GMake, more the "D way", and cross platform ... I think. Not sure exactly how you change that for linking to a .so lib. Cheers, Craig
May 15 2014
parent reply "Craig Dillabaugh" <craig.dillabaugh gmail.com> writes:
On Friday, 16 May 2014 at 01:16:46 UTC, Craig Dillabaugh wrote:
 On Thursday, 15 May 2014 at 22:25:47 UTC, Tom Browder via 
 Digitalmars-d-learn wrote:
 I am a volunteer developer with the well-known 3D CAD FOSS 
 project BRL-CAD:

  http://brlcad.org

 I have wanted to use D for a long time but I hadn't taken the 
 plunge.
 Yesterday I advertised to the BRL-CAD community my new project 
 to
 attempt to create D bindings for BRL-CAD's C libraries, and I 
 created
 a branch for the project.

 I have been looking for specific information on creating D 
 bindings
 from C headers for which there seems to be sufficient 
 information
 available, but I would appreciate recommendations as to the 
 best
 method.  I have successfully built my first pure D program but 
 now
 need to test the feasibility of my project.

 What I have not seen yet is the exact way to build a D program 
 which
 uses D bindings and its matching C library.  I have just 
 created a
 Cookbook page on the D Wiki where I show my first attempt for 
 a real
 GNU Makefile as an example for the project.  The page link is 
 here:

  http://wiki.dlang.org/Using_C_libraries_for_a_D_program

 I would appreciate it if an experienced D user would correct 
 that
 recipe so it should compile the desired binary source correctly
 (assuming no errors in the  input files).

 Thanks for any help.

 Best regards,

 -Tom
Hi Tom, Sadly, I lack the expertise to give you much advice. I did read through your Wiki posting though. One thing that came to mind was you used GMake. Perhaps you should consider using DUB. For example here is the DUB config file for one of my library bindings (in my case I used a static library though): { "name": "shplib", "description": "D bindings for Shapelib. Shapefile reader.", "homepage": "https://github.com/craig-dillabaugh/shplib.d", "homepage": "http://shapelib.maptools.org/", "importPaths":["."], "targetType": "sourceLibrary", "authors": [ "Craig Dillabaugh" ], "sourcePaths": ["./source"], "libs-posix" : ["libshp"] } A little nicer that GMake, more the "D way", and cross platform ... I think. Not sure exactly how you change that for linking to a .so lib. Cheers, Craig
Some info on DUB can be found at: http://code.dlang.org/
May 15 2014
parent reply Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, May 15, 2014 at 8:24 PM, Craig Dillabaugh via
Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 On Friday, 16 May 2014 at 01:16:46 UTC, Craig Dillabaugh wrote:
 On Thursday, 15 May 2014 at 22:25:47 UTC, Tom Browder via
 Digitalmars-d-learn wrote:
...
 What I have not seen yet is the exact way to build a D program which
 uses D bindings and its matching C library.  I have just created a
 Cookbook page on the D Wiki where I show my first attempt for a real
 GNU Makefile as an example for the project.  The page link is here:

  http://wiki.dlang.org/Using_C_libraries_for_a_D_program

 I would appreciate it if an experienced D user would correct that
 recipe so it should compile the desired binary source correctly
 (assuming no errors in the  input files).
...
 Sadly, I lack the expertise to give you much advice.  I did read through
 your Wiki posting though.  One thing that came to mind was you used GMake.
 Perhaps you should consider using DUB.  For example here is the DUB config
 file for one of my library bindings (in my case I used a static library
 though):
... Thanks, Craig, I'll look into dub. Best regards, -Tom
May 15 2014
parent reply "FrankLike" <1150015857 qq.com> writes:
 read through
 your Wiki posting though.  One thing that came to mind was 
 you used GMake.
 Perhaps you should consider using DUB.  For example here is 
 the DUB config
 file for one of my library bindings (in my case I used a 
 static library
 though):
... Thanks, Craig, I'll look into dub. Best regards, -Tom
And use VisualD. Frank
May 15 2014
parent reply Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, May 15, 2014 at 9:56 PM, FrankLike via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
...
 And use  VisualD.
Thanks for the suggestion, Frank, but I don't do windows. Best, -Tom
May 16 2014
parent reply "Alex Herrmann" <alexhairyman gmail.com> writes:
On Friday, 16 May 2014 at 10:10:17 UTC, Tom Browder via
Digitalmars-d-learn wrote:
 Thanks for the suggestion, Frank, but I don't do windows.
Alexander Bothe called Mono-D which is absolutely fantastic and integrates okay with dub too. There is also an emacs major mode for d (d-mode) which gives basic highlighting and indentation too. I use both all of these tools on my Arch linux set up and they work very well, and Mono-D has some debugging support too. Best of luck spreading D Tom! You're doing the lord's work son. Alex
May 16 2014
next sibling parent Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, May 16, 2014 at 1:19 PM, Alex Herrmann via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 On Friday, 16 May 2014 at 10:10:17 UTC, Tom Browder via
...
 Thanks for the suggestion, Frank, but I don't do windows.
...

... Thanks for the suggestions, Alex, ...
 Best of luck spreading D Tom! You're doing the lord's work son.
I don't know if it's the Lord's work or not, but I do think D is the better C++ for lots of us (with profuse thanks to Bjarne Stroustrup for C++). Best regards, -Tom
May 16 2014
prev sibling parent reply "Mengu" <mengukagan gmail.com> writes:
On Friday, 16 May 2014 at 18:19:45 UTC, Alex Herrmann wrote:
 On Friday, 16 May 2014 at 10:10:17 UTC, Tom Browder via
 Digitalmars-d-learn wrote:
 Thanks for the suggestion, Frank, but I don't do windows.
Alexander Bothe called Mono-D which is absolutely fantastic and integrates okay with dub too. There is also an emacs major mode for d (d-mode) which gives basic highlighting and indentation too. I use both all of these tools on my Arch linux set up and they work very well, and Mono-D has some debugging support too. Best of luck spreading D Tom! You're doing the lord's work son. Alex
I am very happy with d-mode in emacs.
May 17 2014
parent Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, May 17, 2014 at 2:28 PM, Mengu via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 On Friday, 16 May 2014 at 18:19:45 UTC, Alex Herrmann wrote:
...
 integrates okay with dub too. There is also an emacs major mode
 for d (d-mode) which gives basic highlighting and indentation
 too. I use both all of these tools on my Arch linux set up and
 they work very well
...
 I am very happy with d-mode in emacs.
Thanks for the reports, Alex and Mengu. I have installed the d-mode for xemacs but I don't have it working properly yet. I'll probably have to ask for help with that before too long. Best, -Tom
May 17 2014
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 15/05/14 23:27, Tom Browder via Digitalmars-d-learn wrote:
 I am a volunteer developer with the well-known 3D CAD FOSS project BRL-CAD:

    http://brlcad.org

 I have wanted to use D for a long time but I hadn't taken the plunge.
 Yesterday I advertised to the BRL-CAD community my new project to
 attempt to create D bindings for BRL-CAD's C libraries, and I created
 a branch for the project.

 I have been looking for specific information on creating D bindings
 from C headers for which there seems to be sufficient information
 available, but I would appreciate recommendations as to the best
 method.
You can use DStep [1] to automatically generate bindings. It requires some manual tweaking afterwards but it will give you a good start. [1] https://github.com/jacob-carlborg/dstep -- /Jacob Carlborg
May 15 2014
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 16 May 2014 at 06:17:47 UTC, Jacob Carlborg wrote:
 On 15/05/14 23:27, Tom Browder via Digitalmars-d-learn wrote:
 I am a volunteer developer with the well-known 3D CAD FOSS 
 project BRL-CAD:

   http://brlcad.org

 I have wanted to use D for a long time but I hadn't taken the 
 plunge.
 Yesterday I advertised to the BRL-CAD community my new project 
 to
 attempt to create D bindings for BRL-CAD's C libraries, and I 
 created
 a branch for the project.

 I have been looking for specific information on creating D 
 bindings
 from C headers for which there seems to be sufficient 
 information
 available, but I would appreciate recommendations as to the 
 best
 method.
You can use DStep [1] to automatically generate bindings. It requires some manual tweaking afterwards but it will give you a good start. [1] https://github.com/jacob-carlborg/dstep
Any plans to get any preprocessor stuff working? Presumably libclang can make this feasible.
May 16 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
John Colvin:

 Any plans to get any preprocessor stuff working?
Do you mean in D? Bye, bearophile
May 16 2014
parent "Mengu" <mengukagan gmail.com> writes:
On Friday, 16 May 2014 at 09:44:11 UTC, bearophile wrote:
 John Colvin:

 Any plans to get any preprocessor stuff working?
Do you mean in D? Bye, bearophile
i think he means in dstep.
May 16 2014
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 16/05/14 11:19, John Colvin wrote:

 Any plans to get any preprocessor stuff working? Presumably libclang can
 make this feasible.
Yes, eventually. Although, currently libclang doesn't really provide an API for the preprocessor, so that needs to be added. -- /Jacob Carlborg
May 16 2014
prev sibling parent Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, May 16, 2014 at 1:17 AM, Jacob Carlborg via
Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
...
 On 15/05/14 23:27, Tom Browder via Digitalmars-d-learn wrote:
...
 I have been looking for specific information on creating D bindings
 from C headers for which there seems to be sufficient information
 available, but I would appreciate recommendations as to the best
 method.
...
 You can use DStep [1] to automatically generate bindings. It requires some
 manual tweaking afterwards but it will give you a good start.

 [1] https://github.com/jacob-carlborg/dstep
Thanks, Jacob. I have seen that, but I've been reluctant to start using that until I get more familiar with D. But I think that is the way to go. Best regards, -Tom
May 16 2014
prev sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Thursday, 15 May 2014 at 22:25:47 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
 I am a volunteer developer with the well-known 3D CAD FOSS 
 project BRL-CAD:

   http://brlcad.org

 I have wanted to use D for a long time but I hadn't taken the 
 plunge.
 Yesterday I advertised to the BRL-CAD community my new project 
 to
 attempt to create D bindings for BRL-CAD's C libraries, and I 
 created
 a branch for the project.

 I have been looking for specific information on creating D 
 bindings
 from C headers for which there seems to be sufficient 
 information
 available, but I would appreciate recommendations as to the best
 method.  I have successfully built my first pure D program but 
 now
 need to test the feasibility of my project.

 What I have not seen yet is the exact way to build a D program 
 which
 uses D bindings and its matching C library.  I have just 
 created a
 Cookbook page on the D Wiki where I show my first attempt for a 
 real
 GNU Makefile as an example for the project.  The page link is 
 here:

   http://wiki.dlang.org/Using_C_libraries_for_a_D_program

 I would appreciate it if an experienced D user would correct 
 that
 recipe so it should compile the desired binary source correctly
 (assuming no errors in the  input files).

 Thanks for any help.

 Best regards,

 -Tom
For a start use dub to build D projects, it's becoming the de-facto build tool. http://code.dlang.org/ Then take a look at one of my projects in which i've ported C headers to D. https://github.com/nomad-software/tcltk In that repo i've included all the C headers as well as their D counterparts for reference. Converting headers is not straightforward when you first start but once you understand the rules it gets easier. Here's some helpful links: http://dlang.org/interfaceToC.html http://www.gamedev.net/page/resources/_/technical/game-programming/binding-d-to-c-r3122 http://forum.dlang.org/thread/qvjjzoxoufxnxzokywgq forum.dlang.org http://forum.dlang.org/thread/wmzqweodmbpkfjbvevcc forum.dlang.org http://forum.dlang.org/thread/fzqloumcqbdvnccvacpi forum.dlang.org Once the D file is created and imported into your program you just need to link the necessary library and you're good to go. See the `package.json` file in the above repository for how i do it for Posix and Windows. Windows DLL's are supplied in the `dist` directory.
May 16 2014
next sibling parent Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, May 16, 2014 at 1:05 PM, Gary Willoughby via
Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 On Thursday, 15 May 2014 at 22:25:47 UTC, Tom Browder via
..
 What I have not seen yet is the exact way to build a D program which
 uses D bindings and its matching C library.  I have just created a
...
 For a start use dub to build D projects, it's becoming the de-facto build
 tool.
... Thanks for all the help, Gary. Wow, you have been very busy--quite a record to strive for! Best regards, -Tom
May 16 2014
prev sibling parent reply Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, May 16, 2014 at 1:05 PM, Gary Willoughby via
Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
...
 Then take a look at one of my projects in which i've ported C headers to D.

 https://github.com/nomad-software/tcltk
I notice your binding source files have a ".d" suffix. Is that the preferred convention? I'm asking because I saw ".di" used on several D Wiki pages. Best, -Tom
May 16 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Friday, 16 May 2014 at 19:05:25 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
 On Fri, May 16, 2014 at 1:05 PM, Gary Willoughby via
 Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 ...
 Then take a look at one of my projects in which i've ported C 
 headers to D.

 https://github.com/nomad-software/tcltk
I notice your binding source files have a ".d" suffix. Is that the preferred convention? I'm asking because I saw ".di" used on several D Wiki pages. Best, -Tom
Using .di is more idiomatic as those are supposed to denote declaration-only interface files (with no implementation). In practice it makes almost no difference though so many people use plain .d by habit.
May 16 2014
parent reply "Gary Willoughby" <dev nomad.so> writes:
On Friday, 16 May 2014 at 19:17:05 UTC, Dicebot wrote:
 On Friday, 16 May 2014 at 19:05:25 UTC, Tom Browder via 
 Digitalmars-d-learn wrote:
 On Fri, May 16, 2014 at 1:05 PM, Gary Willoughby via
 Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 ...
 Then take a look at one of my projects in which i've ported C 
 headers to D.

 https://github.com/nomad-software/tcltk
I notice your binding source files have a ".d" suffix. Is that the preferred convention? I'm asking because I saw ".di" used on several D Wiki pages. Best, -Tom
Using .di is more idiomatic as those are supposed to denote declaration-only interface files (with no implementation). In practice it makes almost no difference though so many people use plain .d by habit.
That's right. I always use .d files when porting C headers because i just see them as regular D code. I like to classify .di files as D 'headers' generated from pure D libraries (using the -H compiler switch). That's just my opinion though and to be honest i don't think it matters. :)
May 16 2014
parent reply Tom Browder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, May 16, 2014 at 2:31 PM, Gary Willoughby via
Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 On Friday, 16 May 2014 at 19:17:05 UTC, Dicebot wrote:
 Using .di is more idiomatic as those are supposed to denote
 declaration-only interface files (with no implementation). In practice it
 makes almost no difference though so many people use plain .d by habit.
...
 That's right. I always use .d files when porting C headers because i just
 see them as regular D code. I like to classify .di files as D 'headers'
 generated from pure D libraries (using the -H compiler switch). That's just
 my opinion though and to be honest i don't think it matters. :)
Okay, Dicebot and Gary, that makes good sense I think, thanks. So I should use the ".d" for the binding source files since there will almost certainly be implementation code in them. Best, -Tom
May 16 2014
parent "Gary Willoughby" <dev nomad.so> writes:
On Friday, 16 May 2014 at 20:28:31 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
 On Fri, May 16, 2014 at 2:31 PM, Gary Willoughby via
 Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:
 On Friday, 16 May 2014 at 19:17:05 UTC, Dicebot wrote:
 Using .di is more idiomatic as those are supposed to denote
 declaration-only interface files (with no implementation). In 
 practice it
 makes almost no difference though so many people use plain .d 
 by habit.
...
 That's right. I always use .d files when porting C headers 
 because i just
 see them as regular D code. I like to classify .di files as D 
 'headers'
 generated from pure D libraries (using the -H compiler 
 switch). That's just
 my opinion though and to be honest i don't think it matters. :)
Okay, Dicebot and Gary, that makes good sense I think, thanks. So I should use the ".d" for the binding source files since there will almost certainly be implementation code in them. Best, -Tom
Yeah , I do and Deimos does too: https://github.com/D-Programming-Deimos
May 16 2014