Thread overview
Completely Remove C Runtime with DMD for win32
Jul 15, 2018
tcb
Jul 15, 2018
kinke
Jul 15, 2018
Walter Bright
Jul 15, 2018
tcb
Jul 16, 2018
Walter Bright
Jul 16, 2018
Jonathan Marler
July 15, 2018
I've been trying to compile a trivial program (extern C int main() {return 0;}) without linking parts of the C runtime with no success.

I compile with dmd -debuglib= -defaultlib= -v -L=/INFORMATION -betterC but optlink shows a lot of things from snn.lib being pulled in and the resultant executable is about 12kb. I also replaced object.d with an empty module.

If I pass /nodefaultlib to the linker I get warning 23: no stack and __acrtused_con is undefined so the linker fails with no start address.

Is it possible to completely remove the C runtime on windows, and if so how? Sorry for the sloppily formatted post.
July 15, 2018
On Sunday, 15 July 2018 at 20:29:29 UTC, tcb wrote:
> Is it possible to completely remove the C runtime on windows, and if so how?

This works for me:

extern(C) int mainCRTStartup() { return 0; }

dmd -m32mscoff -betterC -L/subsystem:CONSOLE main.d

=> 1.5 kB .exe.
July 15, 2018
On 7/15/2018 1:29 PM, tcb wrote:
> I've been trying to compile a trivial program (extern C int main() {return 0;}) without linking parts of the C runtime with no success.

Declaring a C function called "main" causes the C runtime library to be pulled in. After all, main() is not the program entry point, as main() requires a number of things to be set up first (such as argc and argv).

Call the function something else, and compile with -betterC.
July 15, 2018
Thank you both, that clarifies a lot!
July 15, 2018
On 7/15/2018 2:46 PM, tcb wrote:
> Thank you both, that clarifies a lot!

You can set the start address with the DMC C compiler using a #pragma:

  https://www.digitalmars.com/ctg/pragmas.html#startaddress

but currently that isn't directly settable with the DMD compiler.

You can see how it is used in this file of the DMC runtime library:

  https://github.com/DigitalMars/dmc/blob/master/src/WIN32/constart.c#L88
July 16, 2018
On Sunday, 15 July 2018 at 20:29:29 UTC, tcb wrote:
> I've been trying to compile a trivial program (extern C int main() {return 0;}) without linking parts of the C runtime with no success.
>
> I compile with dmd -debuglib= -defaultlib= -v -L=/INFORMATION -betterC but optlink shows a lot of things from snn.lib being pulled in and the resultant executable is about 12kb. I also replaced object.d with an empty module.
>
> If I pass /nodefaultlib to the linker I get warning 23: no stack and __acrtused_con is undefined so the linker fails with no start address.
>
> Is it possible to completely remove the C runtime on windows, and if so how? Sorry for the sloppily formatted post.

I recently created an issue that included an example that allows you to compile a Hello World program on linux x64 without the c standard library, druntime or phobos.

https://issues.dlang.org/show_bug.cgi?id=19078

You can modify it to run on windows as well.  I'm not sure if the _start assembly implementation would be the same on windows.  Try it out and let me know how it works.