Why using wrappers for D?
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Oct 3 10:56:46 PDT 2016
On 10/03/2016 07:19 PM, Chalix wrote:
> If I "import foo;" in my project, it will be compiled alongside.
Not necessarily. dmd won't compile foo unless you tell it to by putting
foo.d on the command line. If foo is only imported, dmd parses the file
but it doesn't compile it.
> So
> there is no need for an extra library.
You don't *need* the library. You can just compile the file together
with the rest of your program, yes.
When you do precompile to a library, you can skip the compilation later.
That can save time.
And you can shorten the distributed D source files to just the function
signatures, like with header files in C. For this, the compiler
recognizes the .di filename extension. This can be used to somewhat
protect closed source code.
And then there are shared libraries, which are loaded at run time. Their
code is not included in the executable, so you get a smaller file. Makes
sense with popular libraries that are used by many programs.
> Same should be for wrapfoo.d. If
> I "import wrapfoo;", I should just need the C-library "foo", and no
> D-library "food" right?
If wrapfoo is really just a binding, i.e. it provides only function
signatures and no implementation, then there's no point in compiling it
to a library.
Otherwise, if wrapfoo actually implements something of its own, it can
make sense to compile it to a library. Just like with every other piece
of code.
> To have a more practical example, I looked up the "header" of the GtkD
> gtk/Main.d file. There are functions exactly like you described:
>
>> public static void init(ref string[] argv)
>> {
>> int argc = cast(int)argv.length;
>> char** outargv = Str.toStringzArray(argv);
>>
>> gtk_init(&argc, &outargv);
>>
>> argv = Str.toStringArray(outargv, argc);
>> }
>
> This function wraps the C-like gtk_init function to a D init function.
> The gtk_init function is the function from the GTK+ library, which is
> loaded in the gtkc/gtk.d file:
>> Linker.link(gtk_init, "gtk_init", LIBRARY.GTK);
> Linker and link are defined in the gtkc/Loader.d
>
> So, why is it not enough just to "import gtk.Main"? What kind of code is
> inside the gtkd-3 library?
If the GtkD files contain all the implementations (not just signatures),
then you don't have to build/use the library. You can just compile the
GtkD source files along with your program, and link with the C library.
More information about the Digitalmars-d-learn
mailing list