digitalmars.D - Remove CRT (C's runtime) from betterC binaries?
- Rel (3/3) Aug 14 2018 Can I or is it even possible to remove the CRT (C's runtime
- rikki cattermole (3/5) Aug 14 2018 Sure, but you can't let dmd link and it'll be a right pain (at least on
- Kagamin (5/12) Aug 15 2018 It's actually much easier to live without crt on windows because
- Rel (12/15) Aug 14 2018 Okey, it seems I figure out how to do it with MinGW linker:
- Walter Bright (2/15) Aug 14 2018 printf() and exit() are part of the CRT.
- Rel (6/7) Aug 14 2018 Well, yes, but there is implementation for them in msvcrt.dll,
- Seb (3/6) Aug 14 2018 Have a look at example 3 of the 2.079 changelog:
- Mike Franklin (65/68) Aug 14 2018 -betterC currently expects the C standard library, and
- Rel (6/9) Aug 14 2018 Well, what would be the difference between betterC and writing my
- Mike Franklin (23/32) Aug 15 2018 It depend on what you want to do. The "minimal runtime"
- Allen Garvey (6/9) Aug 14 2018 If you use either the current beta or master branch of LDC I
Can I or is it even possible to remove the CRT (C's runtime library) completely from my executables compiled with betterC flag?
Aug 14 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.
Aug 14 2018
On Tuesday, 14 August 2018 at 13:34:51 UTC, rikki cattermole wrote:On 15/08/2018 1:11 AM, Rel wrote: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.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.
Aug 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?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
Aug 14 2018
On 8/14/2018 6:37 AM, Rel wrote:On Tuesday, 14 August 2018 at 13:11:57 UTC, Rel wrote:printf() and exit() are part of the CRT.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); }
Aug 14 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.
Aug 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
Aug 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"); } $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
Aug 14 2018
There are other ways to do minimalist programming in D without -betterC. See https://dlang.org/changelog/2.079.0.html#minimal_runtimeWell, 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.
Aug 14 2018
On Wednesday, 15 August 2018 at 06:21:39 UTC, Rel wrote:It depend on what you want to do. The "minimal runtime" approach, for lack of a better description, means you are free to implement just the language features you need in a pay-as-you-go fashion. I prefer it as a way to incrementally port D to a new platform. You originally stated that you didn't want to link in the C runtime, but I see you are using Windows which seems to have a different definition of what the comprises the C Runtime than Linux. In Linux, the C Runtime and the C Standard Library are different things, but Windows seems to think they are the same. I guess I'll stop hear, as I'm probably getting off an a tangent you don't really care about. Just be aware that dmd calls the linker automatically, and automatically links in the C Runtime and the C Standard Library. If you want to avoid those, you'll probably have to compile with -c and link separately.There are other ways to do minimalist programming in D without -betterC. See https://dlang.org/changelog/2.079.0.html#minimal_runtimeWell, 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.Yes, you'll need to copy implementation code from https://github.com/dlang/druntime/blob/master/src/object.d if you want those features. With this approach you become responsible for the runtime implementation, and with great power comes great responsibility. Mike
Aug 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
Aug 14 2018