Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 02, 2018 Example of using C API from D? | ||||
---|---|---|---|---|
| ||||
Attachments:
| I am rewriting a C++ program in D, but need to access a C library that has no D binding: this is a GtkD based program which has a Pango binding, but Pango doesn't offer the information I need, that is hidden in the underlying Fontconfig C API.
I could create a complete D binding for Fontconfig using the GIR files but that seems a bit over the top.
Can anyone point me at an example of a D program using a C API that has structs, enums and functions so I can see if I just hack enough for my use or go on to the full binding activity.
--
Russel.
===========================================
Dr Russel Winder t: +44 20 7585 2200
41 Buckmaster Road m: +44 7770 465 077
London SW11 1EN, UK w: www.russel.org.uk
|
September 03, 2018 Re: Example of using C API from D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | On 03/09/2018 12:52 AM, Russel Winder wrote: > I am rewriting a C++ program in D, but need to access a C library that > has no D binding: this is a GtkD based program which has a Pango > binding, but Pango doesn't offer the information I need, that is hidden > in the underlying Fontconfig C API. > > I could create a complete D binding for Fontconfig using the GIR files > but that seems a bit over the top. > > Can anyone point me at an example of a D program using a C API that has > structs, enums and functions so I can see if I just hack enough for my > use or go on to the full binding activity. > You don't need to create a complete binding for something to use a subset of it. Writing up a Derelict style binding is easy enough since e.g. SharedLib struct handles most of the work (from util package). https://github.com/DerelictOrg/DerelictUtil/blob/master/source/derelict/util/sharedlib.d#L118 |
September 02, 2018 Re: Example of using C API from D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | On Sunday, 2 September 2018 at 12:52:11 UTC, Russel Winder wrote: > I am rewriting a C++ program in D, but need to access a C library that has no D binding: this is a GtkD based program which has a Pango binding, but Pango doesn't offer the information I need, that is hidden in the underlying Fontconfig C API. > > I could create a complete D binding for Fontconfig using the GIR files but that seems a bit over the top. > > Can anyone point me at an example of a D program using a C API that has structs, enums and functions so I can see if I just hack enough for my use or go on to the full binding activity. You can look at zmqd[1] as an example. I've been using it in production. I've also used dstep[2] to translate C headers to D. [1] https://github.com/kyllingstad/zmqd [2] https://github.com/jacob-carlborg/dstep |
September 02, 2018 Re: Example of using C API from D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole Attachments:
| On Mon, 2018-09-03 at 01:00 +1200, rikki cattermole via Digitalmars-d- learn wrote: > […] > You don't need to create a complete binding for something to use a subset of it. True, but all too often you find there are so many interdependencies of names, you end up binding most of the API. I tried fiddling with Fontconfig using Python which has no binding and was able to hack up just enough using CFFI to get things working. So I think in this case a subset for the application is feasible – as opposed to creating a complete binding. > Writing up a Derelict style binding is easy enough since e.g. > SharedLib > struct handles most of the work (from util package). > > https://github.com/DerelictOrg/DerelictUtil/blob/master/source/derelict/util/sharedlib.d#L118 I am not convinced this is a good approach since you do not get the signatures at compile time. The advantage of a binding, or subset of a binding is that you get full compiler support. -- Russel. =========================================== Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk |
September 02, 2018 Re: Example of using C API from D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arun Chandrasekaran Attachments:
| On Sun, 2018-09-02 at 15:40 +0000, Arun Chandrasekaran via Digitalmars- d-learn wrote: > […] > > You can look at zmqd[1] as an example. I've been using it in production. I've also used dstep[2] to translate C headers to D. > > [1] https://github.com/kyllingstad/zmqd > [2] https://github.com/jacob-carlborg/dstep zmqd itself is just a thin D wrapper around deimos.zmq which is where the C stuff is. Looks like a lot of repetition, and it is manual – but very useful for the task of the moment, so thanks for the pointer. For 0MQ, using DStep might be better than the hand crafted zmqd/diemos.zmq. I am not sure if DStep is the right tool for creating a complete D binding to Fontconfig. Given that Fontconfig has a GIR file, using girtod may well be the better route. -- Russel. =========================================== Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk |
September 02, 2018 Re: Example of using C API from D? | ||||
---|---|---|---|---|
| ||||
Attachments:
| On Sun, 2018-09-02 at 18:11 +0100, Russel Winder wrote: > […] > I am not sure if DStep is the right tool for creating a complete D > binding to Fontconfig. Given that Fontconfig has a GIR file, using > girtod may well be the better route. It turns out that the GIR file is not usable, and so the girtod route is not feasible. I shall try the DStep route. Failing that it seems there is https://github.com/WebFreak001/fontconfig-d which is a manual transform of a snapshot of the C API, so not an ideal way, but a definite backstop position. It seems someone has trodden the "using Fontconfig in D" path before me. -- Russel. =========================================== Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk |
September 02, 2018 Re: DStep rocks [was Example of using C API from D?] | ||||
---|---|---|---|---|
| ||||
Attachments:
| On Sun, 2018-09-02 at 18:28 +0100, Russel Winder wrote: > […] > It turns out that the GIR file is not usable, and so the girtod route is not feasible. I shall try the DStep route. Failing that it seems there is > > https://github.com/WebFreak001/fontconfig-d > > which is a manual transform of a snapshot of the C API, so not an > ideal > way, but a definite backstop position. It seems someone has trodden > the > "using Fontconfig in D" path before me. I compiled DStep master/HEAD (v0.2.3-16-g1308991) against LLVM 6.0 and it seems to have done a rather splendid job of creating a D binding to Fontconfig. Low-level obviously, but Fontconfig is seriously low level anyway. Now to work out how to make the project auto generate this D module so as to avoid having it in the repository, and potentially inconsistent with the platform in use. -- Russel. =========================================== Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk |
September 02, 2018 Re: DStep rocks [was Example of using C API from D?] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | On Sunday, 2 September 2018 at 17:49:45 UTC, Russel Winder wrote: > On Sun, 2018-09-02 at 18:28 +0100, Russel Winder wrote: >> > […] >> It turns out that the GIR file is not usable, and so the girtod route is not feasible. I shall try the DStep route. Failing that it seems there is >> >> https://github.com/WebFreak001/fontconfig-d >> >> which is a manual transform of a snapshot of the C API, so not an >> ideal >> way, but a definite backstop position. It seems someone has trodden >> the >> "using Fontconfig in D" path before me. > > I compiled DStep master/HEAD (v0.2.3-16-g1308991) against LLVM 6.0 and it seems to have done a rather splendid job of creating a D binding to Fontconfig. Low-level obviously, but Fontconfig is seriously low level anyway. > > Now to work out how to make the project auto generate this D module so as to avoid having it in the repository, and potentially inconsistent with the platform in use. You could also look at dpp. That's worked for most things I tried and was written in part to avoid the problem of macros changing behaviour at build time. Example here: https://run.dlang.io/?compiler=dmd&source=%23include%20<stdio.h>%0Avoid%20main()%20%7B%0A%20%20%20%20printf("Hello%20dpp.");%0A%7D https://github.com/atilaneves/dpp |
September 03, 2018 Re: Example of using C API from D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | On 03/09/2018 5:07 AM, Russel Winder wrote: > On Mon, 2018-09-03 at 01:00 +1200, rikki cattermole via Digitalmars-d- > learn wrote: >> > […] >> You don't need to create a complete binding for something to use a >> subset of it. > > True, but all too often you find there are so many interdependencies of > names, you end up binding most of the API. I tried fiddling with > Fontconfig using Python which has no binding and was able to hack up > just enough using CFFI to get things working. So I think in this case a > subset for the application is feasible – as opposed to creating a > complete binding. You won't need to actually fill out any c struct's that you don't need either. Make them opaque as long as they are referenced via pointer and not by value. >> Writing up a Derelict style binding is easy enough since e.g. >> SharedLib >> struct handles most of the work (from util package). >> >> > https://github.com/DerelictOrg/DerelictUtil/blob/master/source/derelict/util/sharedlib.d#L118 > > I am not convinced this is a good approach since you do not get the > signatures at compile time. The advantage of a binding, or subset of a > binding is that you get full compiler support. Ugh, you do know that the linker which does all the hard work doesn't know anything about the signature of the C function? That is the part SharedLib replaces. You will of course define it with a proper signature on D's side with a helpful cast :) i.e. https://github.com/DerelictOrg/DerelictGL3/blob/master/source/derelict/opengl/versions/gl1x.d |
September 03, 2018 Re: DStep rocks [was Example of using C API from D?] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc Attachments:
| On Sun, 2018-09-02 at 21:54 +0000, Laeeth Isharc via Digitalmars-d- learn wrote: > On Sunday, 2 September 2018 at 17:49:45 UTC, Russel Winder wrote: > > […] > > Now to work out how to make the project auto generate this D module so as to avoid having it in the repository, and potentially inconsistent with the platform in use. Turns out this is easy with Meson and SCons. Dub is giving some issues. :-( > You could also look at dpp. That's worked for most things I tried and was written in part to avoid the problem of macros changing behaviour at build time. > > Example here: > > https://run.dlang.io/?compiler=dmd&source=%23include%20<stdio.h>%0Avoid%20main()%20%7B%0A%20%20%20%20printf("Hello%20dpp.");%0A%7D > > https://github.com/atilaneves/dpp Interesting alternative to DStep. I came to D to avoid #include, but… I'll give it a whirl once I can get it compiled on Debian Sid. It seems the libclang-dev package does not install a libclang.so symbolic link, you have to be explicit about which version you want, e.g. libclang- 6.0.so on my Debian Sid installation. -- Russel. =========================================== Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk |
Copyright © 1999-2021 by the D Language Foundation