Season of Docs
Guillaume Piolat
first.last at gmail.com
Mon Mar 18 21:28:05 UTC 2019
On Saturday, 16 March 2019 at 14:30:59 UTC, JN wrote:
>
> Also, both. I'd like to know how to create DLLs when building
> with dub.
It's really not that complicated!
# BUILD
In your dub.json, use the targetType
"targetType": "dynamicLibrary",
Mixin that template for creating a DLL:
-------------- dllmain.d --------------
// Dynamic libraries entry point.
// Basically only needed on Windows, on POSIX the other entry
points are sufficient.
version(Windows)
{
template DLLEntryPoint()
{
const char[] DLLEntryPoint = q{
import core.sys.windows.windef;
import core.sys.windows.dll;
extern (Windows) BOOL DllMain(HINSTANCE hInstance,
ULONG ulReason, LPVOID pvReserved)
{
return true;
}
};
}
}
else
{
template DLLEntryPoint()
{
const char[] DLLEntryPoint = ``;
}
}
---------------------------------------
IMPORTANT: Use `export` for any call you want exported in your
DLL!
(Optional: Use some OS-specific tricks to get a static runtime
instead of an annoying, shared one, for example:
// Windows: There is now a new LDC flag to do this, use it
instead
"lflags-windows-ldc": [
"libcmt.lib",
"/nodefaultlib:msvcrt.lib",
"/nodefaultlib:vcruntime.lib"
],
// Linux
"dflags-linux-dmd": ["-defaultlib=libphobos2.a"],
// OSX, not sure for DMD
"dflags-osx-ldc": ["-static"],
)
# CODE
## I DON'T WANT TO USE THE RUNTIME
Either use -betterC, or (a bit easier) link with the runtime but
never enable it. Mark your entry points "nothrow @nogc", use no
TLS, no global dtor/ctor, and try to live within the @nogc world.
## I WANT TO USE THE RUNTIME
Use LDC special ctor/dtor to call Runtime.initialize() and
Runtime.finalize()
https://wiki.dlang.org/LDC-specific_language_changes#LDC_global_crt_ctor_and_LDC_global_crt_dtor
If you don't use them, you will need some sort of initialization
entry points, or a shady lazy mechanism.
AND THEN on every entry points, attach incoming threads with
`thread_attachThis()` and detach them with `thread_detachThis()`.
Don't keep threads attached when they have quite your dynlib and
might be killed by a host program, you will suffer quite a bit if
you do.
Hope it helps! It has been doable for YEARS to make dynlib with D.
More information about the Digitalmars-d
mailing list