Found when working on rdmd.
test.d.
----
import std.stdio;
static this() { writeln("static this"); }
unittest { writeln("unittest"); }
----
Prints nothing, should print "static this" and "unittest":
----
dmd -main -c -oftest.o -unittest test.d
dmd test.o
./test
----
Comment #1 by andrej.mitrovich — 2016-08-28T14:18:17Z
The problem is the compiler allows -c and -main. It will generate the D main function in a separate module but later when you try to link via `dmd test.obj` you won't be linking with this module.
The workaround is simple, pass -main while linking:
-----
dmd -c -oftest.o -unittest test.d
dmd -main test.o
./test
-----
In the meantime allowing -c and -main in the same invocation should be disallowed.
Comment #2 by ag0aep6g — 2016-08-28T15:09:43Z
(In reply to Andrej Mitrovic from comment #1)
> The problem is the compiler allows -c and -main. It will generate the D main
> function in a separate module but later when you try to link via `dmd
> test.obj` you won't be linking with this module.
I think the module from -main is there when linking, but the one actually given on the command line is discarded/ignored/overwritten. Otherwise, linking should fail because of no main. But linking succeeds and produces a program that doesn't do anything.
[...]
> In the meantime allowing -c and -main in the same invocation should be
> disallowed.
When -of is not given, -c and -main work acceptably together. dmd generates a __main.o for the main module.
Only with -of do things get weird. I don't see how there would be some fundamental problem. dmd happily compiles multiple source files to one object file. So it should be able to add an empty main function to an object file without breaking the other code.
Comment #3 by andrej.mitrovich — 2016-08-28T15:36:56Z
> dmd happily compiles multiple source files to one object file.
Ah I thought this was only a feature of -lib. I forgot about the mix of -c and -of, otherwise it would indeed be separate object files.
Then this issue is more perplexing. Probably best to see what's in the assembler output. :p
Comment #4 by robert.schadek — 2024-12-13T18:49:51Z