On Tuesday, 14 August 2018 at 13:11:57 UTC, Rel wrote:
Can I or is it even possible to remove the CRT (C's runtime library) completely from my executables compiled with betterC flag?

-betterC currently expects the C standard library, and consequently the C runtime. Under the hood DMD calls `gcc` to do the linking. `gcc` automatically links in the C standard library and the C runtime.

You could potentially use the `-L` flag to pass `-nostdlibs`, `-nodefaultlibs`, `-nostartfiles`, and friends, but I don't know if it will work. If you don't want the C runtime or the C standard library the best thing might just be to link separately with a direct invocation of `ld`.

There are other ways to do minimalist programming in D without -betterC. See https://dlang.org/changelog/2.079.0.html#minimal_runtime

The following is an illustration:

---object.d
module object;

alias immutable(char)[] string;

private long __d_sys_write(long arg1, in void* arg2, long arg3)
{
    long result;

    asm
    {
        mov RAX, 1;
        mov RDI, arg1;
        mov RSI, arg2;
        mov RDX, arg3;
        syscall;
    }

    return result;
}

void write(string text)
{
    __d_sys_write(2, text.ptr, text.length);
}

private void __d_sys_exit(long arg1)
{
    asm
    {
        mov RAX, 60;
        mov RDI, arg1;
        syscall;
    }
}

extern void main();
private extern(C) void _start()
{
    main();
    __d_sys_exit(0);
}

---main.d
module main;

void main()
{
    write("Hello, World\n");
}

# On a 64-bit Linux host
$dmd -c -lib -conf= object.d main.d -of=main.o (Note: no -betterC)
$ld main.o -o main
$size main
   text    data     bss     dec     hex filename
    176       0       0     176      b0 main
$main
Hello, World

Mike

Reply via email to