Thread overview | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 03, 2016 Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Hi All! The documentation of D (https://dlang.org/overview.html#compatibility) says: "Direct Access to C API's Not only does D have data types that correspond to C types, it provides direct access to C functions. There is no need to write wrapper functions, parameter swizzlers, nor code to copy aggregate members one by one." So, if there is no need for wrapper functions, why are there a lot of them? For example, GTK+ is a C library, with C-include files. Now there exists the GtkD (http://gtkd.org/) library, which describes itself as a wrapper of GTK+. GtkD contains the .d files (which I need for import, of course) and a seperate library (libgtkd-3.so). If D has direct Access to C API's, why do we need this the gtkd-3 lib, and not just use the gtk-3 lib? Furthermore, if there is an not very popular C library, where no wrapper function exists, would it possible to include it into a D project? Probably I have to transform all the .h to .d files, so i can "import" them instead of "include" them. But then, could I link against the C-library? I did not understand the concept of interaction between C and D, and I am a bit confused about wrapper functions and bindings for D now... Would be great if someone could make it a bit more clear to me :) |
October 03, 2016 Re: Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chalix | On Monday, 3 October 2016 at 12:08:54 UTC, Chalix wrote: > So, if there is no need for wrapper functions, why are there a lot of them? A lot of people like the wrappers as being prettier to use since you can turn C functions into classes and such. gtkd is an example of that. > Furthermore, if there is an not very popular C library, where no wrapper function exists, would it possible to include it into a D project? Of course, it is simple, just bring over the necessary struct, constant, and function prototypes and it just works. |
October 04, 2016 Re: Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chalix | On 04/10/2016 1:08 AM, Chalix wrote: > Hi All! > > The documentation of D (https://dlang.org/overview.html#compatibility) > says: > > "Direct Access to C API's > > Not only does D have data types that correspond to C types, it provides > direct access to C functions. There is no need to write wrapper > functions, parameter swizzlers, nor code to copy aggregate members one > by one." > > > So, if there is no need for wrapper functions, why are there a lot of > them? For example, GTK+ is a C library, with C-include files. Now there > exists the GtkD (http://gtkd.org/) library, which describes itself as a > wrapper of GTK+. GtkD contains the .d files (which I need for import, of > course) and a seperate library (libgtkd-3.so). > If D has direct Access to C API's, why do we need this the gtkd-3 lib, > and not just use the gtk-3 lib? > > Furthermore, if there is an not very popular C library, where no wrapper > function exists, would it possible to include it into a D project? > Probably I have to transform all the .h to .d files, so i can "import" > them instead of "include" them. But then, could I link against the > C-library? > > > I did not understand the concept of interaction between C and D, and I > am a bit confused about wrapper functions and bindings for D now... > Would be great if someone could make it a bit more clear to me :) Ok lets get a few things straight. To use any kind of function you must declare it, plain and simple. Any c or c++ function/class is the very much same way. Now C++ types such as classes are highly limited in D since it doesn't ugh cross over all that well (it does some weird things). These kinds of declarations are called bindings. We must have them since the D compilers don't support reading header files. Why don't they "just" support them you ask? Well simple, that's a whole new frontend that we must support... Walter is quite opposed to the idea and rightly so. So the real interesting question, why do we have wrappers around e.g. c++ libs? Simple, the original C++ code was designed for C++ and we can simply do those interfaces better. --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus |
October 03, 2016 Re: Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chalix | On Monday, 3 October 2016 at 12:08:54 UTC, Chalix wrote:
> Hi All!
>
> The documentation of D (https://dlang.org/overview.html#compatibility) says:
>
> "Direct Access to C API's
>
> Not only does D have data types that correspond to C types, it provides direct access to C functions. There is no need to write wrapper functions, parameter swizzlers, nor code to copy aggregate members one by one."
>
>
> So, if there is no need for wrapper functions, why are there a lot of them? For example, GTK+ is a C library, with C-include files. Now there exists the GtkD (http://gtkd.org/) library, which describes itself as a wrapper of GTK+. GtkD contains the .d files (which I need for import, of course) and a seperate library (libgtkd-3.so).
> If D has direct Access to C API's, why do we need this the gtkd-3 lib, and not just use the gtk-3 lib?
>
> Furthermore, if there is an not very popular C library, where no wrapper function exists, would it possible to include it into a D project? Probably I have to transform all the .h to .d files, so i can "import" them instead of "include" them. But then, could I link against the C-library?
>
>
> I did not understand the concept of interaction between C and D, and I am a bit confused about wrapper functions and bindings for D now...
> Would be great if someone could make it a bit more clear to me :)
D provides ways to do things that C or C++ don't provide (otherwise we wouldn't be using it). C/C++ functions and structures are designed to fit well in C/C++, not in D. To make them easy to use and avoid code of mixed style we build up a facade : the wrapper. That's all.
|
October 03, 2016 Re: Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Monday, 3 October 2016 at 12:12:44 UTC, Adam D. Ruppe wrote: > A lot of people like the wrappers as being prettier to use since you can turn C functions into classes and such. gtkd is an example of that. Thanks for your fast answers :) Hm, I thing I am missing some fundamentals... I read a bit about creating libraries in C++, and I found out, this is not possible, because there is no definition of the API. So you need some C "factory methods" to load C++ classes. So is it possible now by D to load a class directly, or what do you mean by "turn C functions into classes"? Also, I used the Qt library a lot with C++. But although it is a library, I have access to all the classes, like " QWidget w = new QWidget();". There is no factory method used. (This confuses me now a bit...) If its too much to explain within some sentences, maybe you know something where I could read more about it? I did not find anything myself. But I really want to understand what's going on. > Of course, it is simple, just bring over the necessary struct, constant, and function prototypes and it just works. I have to try it :D |
October 03, 2016 Re: Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chalix | On Monday, 3 October 2016 at 12:47:48 UTC, Chalix wrote: > I read a bit about creating libraries in C++, and I found out, this is not possible, because there is no definition of the API. It is possible, you just need to match compilers with the library in C++, whereas C libraries don't need such an exact match. With your Qt library, you get a build of it that is compatible with the compiler you use to build your application (either compiling it yourself or getting it from an OS package repo where they did it for you for your OS version) > you mean by "turn C functions into classes"? Wrap the C functions inside D classes. So they write D code that calls the C functions, then you use their D code. |
October 03, 2016 Re: Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On Monday, 3 October 2016 at 12:15:10 UTC, rikki cattermole wrote: > To use any kind of function you must declare it, plain and simple. > Any c or c++ function/class is the very much same way. > Now C++ types such as classes are highly limited in D since it doesn't ugh cross over all that well (it does some weird things). I don't know if we understand each other. You have to declare each function in the language you are programming, that is clear to me. So if you write in D, you need "D-Headers" (which are not called headers in D, i know). What do you mean with "Now C++ types such as classes are highly limited in D"? > Why don't they "just" support them you ask? Yeah, that a D compiler can't read .h files is obvious to me :) |
October 03, 2016 Re: Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Monday, 3 October 2016 at 12:54:03 UTC, Adam D. Ruppe wrote: > It is possible, you just need to match compilers with the library in C++, whereas C libraries don't need such an exact match. > > With your Qt library, you get a build of it that is compatible with the compiler you use to build your application (either compiling it yourself or getting it from an OS package repo where they did it for you for your OS version) Ah, so the Qt libraries are C++ libraries. And they have to be compiled with the same compiler (or at least with a compiler with the same specification) I use for my application? I didn't know that, but it makes sense to me. But if there would be 2 C++ compilers on my linux system which create 2 different API's, I would need the Qt library twice on my system? One for an application compiled with the one compiler, and the other library for an application compiled with the other compiler. Since I have only 1 Qt library on my system, all linux compilers must create compatible API's, right? > Wrap the C functions inside D classes. So they write D code that calls the C functions, then you use their D code. Ok, this is the same what cym13 wanted to say, right? So you can use the advantages of D? But I don't get, why I have a gtkd-3 lib. Why can't I just link against the gtk-3 lib then? I have now the headers to use the nice D stuff, but the linking should be done against the C-compiled library. |
October 04, 2016 Re: Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chalix | On 04/10/2016 1:56 AM, Chalix wrote: > On Monday, 3 October 2016 at 12:15:10 UTC, rikki cattermole wrote: >> To use any kind of function you must declare it, plain and simple. >> Any c or c++ function/class is the very much same way. >> Now C++ types such as classes are highly limited in D since it doesn't >> ugh cross over all that well (it does some weird things). > > I don't know if we understand each other. You have to declare each > function in the language you are programming, that is clear to me. So if > you write in D, you need "D-Headers" (which are not called headers in D, > i know). > > What do you mean with "Now C++ types such as classes are highly limited > in D"? Basically some features do not match up nicely like operators and constructors. They also do not share semantics e.g. value versus reference. --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus |
October 03, 2016 Re: Why using wrappers for D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chalix | On Monday, 3 October 2016 at 13:12:55 UTC, Chalix wrote:
> But I don't get, why I have a gtkd-3 lib. Why can't I just link against the gtk-3 lib then? I have now the headers to use the nice D stuff, but the linking should be done against the C-compiled library.
If you don't use D-specific stuff like autogenerated comparison operators and big struct initializers, you would be able to link with C library alone.
|
Copyright © 1999-2021 by the D Language Foundation