Using D without libphobos

Mike Franklin slavo5150 at yahoo.com
Thu Apr 26 09:55:15 UTC 2018


On Thursday, 26 April 2018 at 09:24:19 UTC, A. Nicholi wrote:

> So in a way, the D runtime is similar to libstdc++, providing 
> implementations of runtime language features.

I would argue that Phobos is more analogous to libstc++, but 
there are some language features in C++ that are implemented in 
libstc++.  For example, if you try to compile the following code.

---main.cpp
int main()
{
     throw 1;
     return 0;
}

$g++ -c main.cpp
$ld main.o
ld: warning: cannot find entry symbol _start; defaulting to 
00000000004000e8
main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to 
`__cxa_allocate_exception'
main.cpp:(.text+0x1c): undefined reference to `typeinfo for int'
main.cpp:(.text+0x24): undefined reference to `__cxa_throw'

So, yeah those "__cxa_..." functions are very similar to what 
you'll find in Druntim.  D calls them runtime hooks, and you can 
find an unmaintained list of them here:  
https://wiki.dlang.org/Runtime_Hooks

Here's similar experiment in D.  IMO D is actually better than 
C++ in this regard because it emits a compile-time error, instead 
of a linker error, when it can't find something in the runtime 
that it needs.

---object.d
module object;
// this useless object.d file is currently required, though I'm 
trying to
// find a way to get rid of such nonsense.
// See https://github.com/dlang/dmd/pull/7825

---main.d
void main()
{
     throw new Exception("whatever");
}

$dmd -conf= -defaultlib= main.d
main.d(3): Error: Cannot use throw statements because 
object.Throwable was not declared

> But it is also like C++ in that those language features can be 
> avoided, correct?

That was the goal with the "minimal runtime" features released in 
v2.079.  It's still a work in progress though.  See 
https://github.com/dlang/dmd/pull/8204

But, yes, as long as you avoid certain features that require 
runtime implementations in your code, you should still be able to 
write software in D.  And, if I am allowed to have may way, it 
should get even better.

> At least with the use of minimal D, I mean. This means that as 
> a language, there is enough granularity to theoretically 
> provide as few or as many features as one desires for their use 
> case, making the penning of new C and C++ code redundant? Do I 
> get this right?

Not sure if I totally understand that, but it sounds right on.  
The goal with the minimal runtime features is to allow one use D 
on any platform in a pay-as-you-go fashion.  That is, you only 
need to implement what you intend to use.  The primary use case I 
have in mind is using D for resource-constrained 
microcontrollers, though it would also be useful for libraries 
written in D, but intended to be linked in by applications 
written in other languages.  I'm there are also use cases that I 
haven't even thought of.

Mike


More information about the Digitalmars-d mailing list