Jump to page: 1 2
Thread overview
Remove CRT (C's runtime) from betterC binaries?
Aug 14, 2018
Rel
Aug 14, 2018
rikki cattermole
Aug 15, 2018
Kagamin
Aug 14, 2018
Rel
Aug 15, 2018
Walter Bright
Aug 15, 2018
Rel
Aug 14, 2018
Seb
Aug 14, 2018
Mike Franklin
Aug 15, 2018
Rel
Aug 15, 2018
Mike Franklin
Aug 15, 2018
Allen Garvey
August 14, 2018
Can I or is it even possible to remove the CRT (C's runtime library) completely from my executables compiled with betterC flag?
August 15, 2018
On 15/08/2018 1:11 AM, 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?

Sure, but you can't let dmd link and it'll be a right pain (at least on Windows) to not have a libc to deal with the entry point for you.
August 14, 2018
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?

Okey, it seems I figure out how to do it with MinGW linker:

    import core.stdc.stdlib;
    import core.stdc.stdio;

    extern (C) void start() {
        printf("Hello World!");
        exit(0);
    }

    // dmd -c -m32mscoff -betterC -of=test32.obj test.d
    // dmd -c -m64 -betterC -of=test64.obj test.d
    // gcc -o test32.exe -m32 -nostdlib -s test32.obj -lmsvcrt
    // gcc -o test64.exe -m64 -nostdlib -s test64.obj -lmsvcrt
August 14, 2018
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?

Have a look at example 3 of the 2.079 changelog: https://dlang.org/changelog/2.079.0.html#minimal_runtime
August 14, 2018
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
August 14, 2018
On 8/14/2018 6:37 AM, Rel wrote:
> 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?
> 
> Okey, it seems I figure out how to do it with MinGW linker:
> 
>      import core.stdc.stdlib;
>      import core.stdc.stdio;
> 
>      extern (C) void start() {
>          printf("Hello World!");
>          exit(0);
>      }

printf() and exit() are part of the CRT.
August 15, 2018
> printf() and exit() are part of the CRT.

Well, yes, but there is implementation for them in msvcrt.dll, which is installed on all Windows platforms. So I can link to it and use it for free, without adding the whole CRT to my executable. Otherwise I could use MessageBox and ExitProcess for testing hello-world application, which reside in user32.dll and kernel32.dll respectively.

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

Well, what would be the difference between betterC and writing my own minimal runtime? For the time being doing betterC looks preferable, so I don't need to reimplement some runtime stuff. Just recompiling the same program with empty object module gives me few errors like size_t, string and etc not implemented in object module.
August 15, 2018
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?

If you use either the current beta or master branch of LDC I believe they have disabled the C runtime for WebAssembly, but I don't know if it's possible to target other platforms with those same improvements.

https://github.com/ldc-developers/ldc/pull/2787
August 15, 2018
On Tuesday, 14 August 2018 at 13:34:51 UTC, rikki cattermole wrote:
> On 15/08/2018 1:11 AM, 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?
>
> Sure, but you can't let dmd link and it'll be a right pain (at least on Windows) to not have a libc to deal with the entry point for you.

It's actually much easier to live without crt on windows because the system provides most stuff like io, threads and memory heap with nice api.
« First   ‹ Prev
1 2