Using D without libphobos
Mike Franklin
slavo5150 at yahoo.com
Thu Apr 26 03:53:54 UTC 2018
On Thursday, 26 April 2018 at 03:04:55 UTC, A. Nicholi wrote:
> I am working on a large cross-platform project, which will be
> written primarily in D, interfacing to C as necessary. To get
> finer control over memory safety, binary size, and use of the
> GC, we would like to disclude libphobos as a dependency in lieu
> of our own code. The project is compiled using LDC.
>
> I am not sure if this is possible though, as it seems there are
> certain things in libphobos that are tightly coupled into the D
> runtime. There are several things in the core namespace that
> would be helpful for us (SIMD, C bindings, etc), but I am not
> sure if that is not also part of libphobos along with the other
> namespaces.
>
> How do I remove libphobos as a runtime dependency with ld and
> MSVC’s link.exe? Is it possible to decouple core from other
> parts of the runtime, and if so, how?
I suggest reading the following 2 items before digging deeper:
https://dlang.org/blog/2017/08/23/d-as-a-better-c/
https://dlang.org/changelog/2.079.0.html#minimal_runtime
Next you should realize that Phobos and DRuntime are actually 2
different things that are, unfortunately, often compiled together
into one "libphobos2" binary when the compiler is released.
Phobos is the standard library:
https://github.com/dlang/phobos
DRuntime contains C language bindings, OS bindings, and some D
language feature implementations, including the GC.
https://github.com/dlang/druntime
It's quite unfortunate there so much hard coupling between all of
these components, but that's the way it is. Phobos depends on
DRuntime. DRuntime depends on C language bindings and OS
bindings.
Compiling the simplest of programs with DMD
(https://run.dlang.io/is/KMjKsJ) results in the following C
compiler invocation:
------
cc onlineapp.o -o /tmp/dmd_run3nK4IS -g -m64 -Xlinker -v
-L/dlang/dmd/linux/bin64/../lib64 -Xlinker --export-dynamic
-Xlinker -Bstatic -lphobos2 -Xlinker -Bdynamic -lpthread -lm -lrt
-ldl
Which in turn calls the following linker invocation:
------
GNU gold (GNU Binutils for Ubuntu 2.29.1) 1.14
collect2 version 7.2.0
/usr/bin/ld -plugin
/usr/lib/gcc/x86_64-linux-gnu/7/liblto_plugin.so
-plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
-plugin-opt=-fresolution=/tmp/ccg3GKxT.res
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc
-plugin-opt=-pass-through=-lgcc_s --sysroot=/ --build-id
--eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed
-dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro
-o /tmp/dmd_run3nK4IS
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o
-L/dlang/dmd/linux/bin64/../lib64
-L/usr/lib/gcc/x86_64-linux-gnu/7
-L/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu
-L/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib
-L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu
-L/usr/lib/../lib -L/dlang/dmd/linux/lib64
-L/usr/lib/gcc/x86_64-linux-gnu/7/../../.. onlineapp.o -v
--export-dynamic -Bstatic -lphobos2 -Bdynamic -lpthread -lm -lrt
-ldl -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc
--as-needed -lgcc_s --no-as-needed
/usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
The compiler uses the C compiler (unfortunately again) to do its
linking; and it becomes evident that D is in some ways a layer on
top of C.
You can compile some D programs without linking libphobos2, but
will require separate compilation and linking because the
compiler itself actually hard-codes the call to the linker
(actually the C compiler as demonstrated above). Example 3 at
https://dlang.org/changelog/2.079.0.html#minimal_runtime
demonstrates this.
If you use that method, you won't be able to use certain features
of D that have runtime implementations. The obvious ones are
classes, dynamic arrays, and exceptions.
I could go on, but I'd have to make some assumptions about what
you're really after. Feel free to ask more specific questions
and I'll be happy to share what I know (or at least what I think
I know; sometimes I'm wrong).
Mike
More information about the Digitalmars-d
mailing list