Thread overview
[Issue 16440] wrong code with -main -c -of
Aug 28, 2016
Andrej Mitrovic
Aug 28, 2016
ag0aep6g@gmail.com
Aug 28, 2016
Andrej Mitrovic
Dec 17, 2022
Iain Buclaw
August 28, 2016
https://issues.dlang.org/show_bug.cgi?id=16440

Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |accepts-invalid
                 CC|                            |andrej.mitrovich@gmail.com
           Hardware|x86_64                      |All
                 OS|Linux                       |All

--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> ---
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.

--
August 28, 2016
https://issues.dlang.org/show_bug.cgi?id=16440

--- Comment #2 from ag0aep6g@gmail.com ---
(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.

--
August 28, 2016
https://issues.dlang.org/show_bug.cgi?id=16440

--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> ---
> 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

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=16440

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--