www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Example of using C API from D?

reply Russel Winder <russel winder.org.uk> writes:
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.=20

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.

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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
Sep 02 2018
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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
Sep 02 2018
parent reply Russel Winder <russel winder.org.uk> writes:
On Mon, 2018-09-03 at 01:00 +1200, rikki cattermole via Digitalmars-d-
learn wrote:
=20
[=E2=80=A6]
 You don't need to create a complete binding for something to use a=20
 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 =E2=80=93 as opposed to creating a complete binding.=20
 Writing up a Derelict style binding is easy enough since e.g.
 SharedLib=20
 struct handles most of the work (from util package).
=20
=20
https://github.com/DerelictOrg/DerelictUtil/blob/master/source/derelict/uti= l/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. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Sep 02 2018
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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
Sep 02 2018
parent Russel Winder <russel winder.org.uk> writes:
On Mon, 2018-09-03 at 11:41 +1200, rikki cattermole via Digitalmars-d-learn
wrote:
[=E2=80=A6]
=20
 You won't need to actually fill out any c struct's that you don't need=
=20
 either. Make them opaque as long as they are referenced via pointer and=
=20
 not by value.
True. And indeed Fontconfig can mostly meet the "it's an opaque type based system" but there are a few dark corners =E2=80=93 most of which I need!= =20 [=E2=80=A6]
=20
 Ugh, you do know that the linker which does all the hard work doesn't=20
 know anything about the signature of the C function? That is the part=20
 SharedLib replaces. You will of course define it with a proper signature=
=20
 on D's side with a helpful cast :)
Not good enough to be honest, D is a statically typed language and so all usage of functions should be checked against something other than a human guess. Anyway DStep has given me a Fontconfig D module that works nicely, and I wi= ll check dpp as an alternative as soon as it builds on Debian Sid. =20 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Sep 06 2018
prev sibling parent reply Arun Chandrasekaran <aruncxy gmail.com> writes:
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
Sep 02 2018
next sibling parent Russel Winder <russel winder.org.uk> writes:
On Sun, 2018-09-02 at 15:40 +0000, Arun Chandrasekaran via Digitalmars-
d-learn wrote:
 [=E2=80=A6]
=20
 You can look at zmqd[1] as an example. I've been using it in=20
 production. I've also used dstep[2] to translate C headers to D.
=20
 [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 =E2=80=93 = 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.=20 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Sep 02 2018
prev sibling next sibling parent Russel Winder <russel winder.org.uk> writes:
On Sun, 2018-09-02 at 18:11 +0100, Russel Winder wrote:
 [=E2=80=A6]
 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.=20
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. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Sep 02 2018
prev sibling parent reply Russel Winder <russel winder.org.uk> writes:
On Sun, 2018-09-02 at 18:28 +0100, Russel Winder wrote:
=20
[=E2=80=A6]
 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
=20
 https://github.com/WebFreak001/fontconfig-d
=20
 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. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Sep 02 2018
parent reply Laeeth Isharc <Laeeth laeeth.com> writes:
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
Sep 02 2018
parent reply Russel Winder <russel winder.org.uk> writes:
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:
=20
[=E2=80=A6]
 Now to work out how to make the project auto generate this D=20
 module so as to avoid having it in the repository, and=20
 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=20
 and was written in part to avoid the problem of macros changing=20
 behaviour at build time.
=20
 Example here:
=20
=20
https://run.dlang.io/?compiler=3Ddmd&source=3D%23include%20<stdio.h>%0Avoid= %20main()%20%7B%0A%20%20%20%20printf("Hello%20dpp.");%0A%7D
=20
 https://github.com/atilaneves/dpp
Interesting alternative to DStep. I came to D to avoid #include, but=E2=80= =A6 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. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Sep 03 2018
parent reply Andrea Fontana <nospam example.com> writes:
On Monday, 3 September 2018 at 10:50:17 UTC, Russel Winder wrote:
 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.
I use dpp to generate d code to import: - Create a temp.dpp file with #include inside - Run "d++ --preprocess-only temp.dpp" Now you have your .d file exactly like in dstep. Andrea
Sep 03 2018
parent Russel Winder <russel winder.org.uk> writes:
On Mon, 2018-09-03 at 12:45 +0000, Andrea Fontana via Digitalmars-d-
learn wrote:
 [=E2=80=A6]
=20
 I use dpp to generate d code to import:
 - Create a temp.dpp file with #include inside
 - Run "d++ --preprocess-only temp.dpp"
=20
 Now you have your .d file exactly like in dstep.
=20
So where does this leave DStep if DPP is the next generation tool? Of course I can create a dstep executable, but cannot yet create a d++ executable on Debian Sid. :-( --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Sep 03 2018