Bug 11008 – Allow -main switch even if user-defined main function exists
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-10T21:18:51Z
Last change time
2021-09-08T19:16:00Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
Kenji Hara
Comments
Comment #0 by k.hara.pg — 2013-09-10T21:18:51Z
Currently, if user-defined main() function exists, specifying -main switch will fail to compile.
// test.d
void main() {}
$ dmd -main -run test
test.d(1): Error: only one main/WinMain/DllMain allowed, -main switch added another main()
But I think this is unnecessary restriction. Compiler can stop adding "another main" when user-defined main() detected.
Comment #1 by issues.dlang — 2013-09-10T21:27:32Z
It could also be argued that it's misleading if -main works when there's already a main function, because instead of simply running the unit tests, the main is going to do whatever the already included main function does. Giving the user an error alerts them to the fact that they're trying to add a main when there already is one (one which almost certainly doesn't do what they want). So, it seems to me that not having -main error out gains us little and causes problems when someone tries to use -main when they already have a main function defined.
Comment #2 by k.hara.pg — 2013-09-10T21:49:12Z
Ok. I withdraw this.
Comment #3 by maxim — 2013-09-10T21:53:07Z
One way to do this is to introduce separate option, say -fno-main-error which does what was proposed in this enh without screwing current switch.
Comment #4 by thelastmammoth — 2013-09-10T22:05:42Z
(In reply to comment #3)
> One way to do this is to introduce separate option, say -fno-main-error which
> does what was proposed in this enh without screwing current switch.
(In reply to comment #3)
> One way to do this is to introduce separate option, say -fno-main-error which
> does what was proposed in this enh without screwing current switch.
something more general and useful would be:
dmd -main=mypck.mymodule.myfunction [...]
which specifies the main function to use.
Comment #5 by r.sagitario — 2013-09-10T23:42:09Z
What about something like -emptymain to actually force an empty main function to replace an optionally existing one. That way you'd be be able to run the unittests without executing anything else?
Comment #6 by petar.p.kirov — 2021-03-31T05:10:19Z
Comment #7 by petar.p.kirov — 2021-03-31T05:11:54Z
Also, if it is deemed that this would be a breaking change, than I suggest we go with Rainer's suggestion for `-emptymain`.
Comment #8 by john.michael.hall — 2021-05-01T19:34:03Z
@ZombieDev As a heads up, following [1], the following code in run.dlang.org [2] produces the only one main error due to relying on dub test --single.
```d
/+dub.sdl:
dependency "mir-algorithm" version="*"
+/
import core.stdc.stdio;
unittest
{
printf("From unittest\n");
}
void main()
{
printf("From main\n");
}
```
[1] https://github.com/dlang-tour/core-exec/pull/56
[2] https://run.dlang.io/is/0PlIZU
Comment #9 by dlang-bot — 2021-09-08T11:23:03Z
@dkorpel created dlang/dmd pull request #13057 "Fix issue 11008 - Allow -main switch even if user-defined main function exists" fixing this issue:
- fix issue 11008 - Allow -main switch even if user-defined main function exists
https://github.com/dlang/dmd/pull/13057
Comment #10 by dlang-bot — 2021-09-08T19:16:00Z
dlang/dmd pull request #13057 "Fix issue 11008 - Allow -main switch even if user-defined main function exists" was merged into master:
- c4fa7709ee2bc93bbf071335bfb7027ea3eaf527 by dkorpel:
fix issue 11008 - Allow -main switch even if user-defined main function exists
https://github.com/dlang/dmd/pull/13057