Bug 9723 – Implement -main switch to inject a default main() function

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-03-14T14:32:00Z
Last change time
2013-04-12T10:08:29Z
Keywords
preapproved
Assigned to
nobody
Creator
bugzilla

Comments

Comment #0 by bugzilla — 2013-03-14T14:32:08Z
When compiling a module with -unittest, and an exe file is being generated, and there is no main() function, add: int main() { return 0; } and compile it in.
Comment #1 by andrej.mitrovich — 2013-03-14T14:55:25Z
How can you reasonably implement this if you pass object files or static libraries to DMD which might contain main? E.g.: foo.d: import bar; void main() { } bar.d: unittest { assert(0); } $ dmd -c foo.d $ dmd -unittest -ofbar.exe foo.d bar.obj You could end up getting linker errors if DMD tries to add another main function.
Comment #2 by bugzilla — 2013-03-14T16:59:40Z
Good point. You can deal with it by not doing it if there are any explicit .o files on the command line. No need to worry about libraries with main() in it - nothing to pull it in.
Comment #3 by andrej.mitrovich — 2013-03-14T17:02:11Z
(In reply to comment #2) > No need to worry about libraries with main() in it - > nothing to pull it in. What do you mean by that? Also we have --main in RDMD that is used exactly for this purpose. Why not just move this switch over to DMD instead of doing magic that stops working as soon as you add separately-compiled dependencies?
Comment #4 by andrej.mitrovich — 2013-03-14T17:03:12Z
(In reply to comment #3) > (In reply to comment #2) > > No need to worry about libraries with main() in it - > > nothing to pull it in. > > What do you mean by that? > > Also we have --main in RDMD that is used exactly for this purpose. Why not just > move this switch over to DMD instead of doing magic that stops working as soon > as you add separately-compiled dependencies? Also, by doing it as a switch we can keep compatibility with RDMD, all it has to do is call DMD's -main switch instead of making a separate file with main in it. So old code continues to work.
Comment #5 by bugzilla — 2013-03-14T17:39:32Z
I mean that if you've installed main() in a library file, it won't get pulled in unless you have a reference to main somewhere else, which is awfully unlikely and I've never seen it.
Comment #6 by dlang-bugzilla — 2013-03-14T17:49:04Z
Some libraries define main(), and let the user define their own entry point. E.g. libfoo could have a foomain() declaration, and the user has to implement it. I believe SDL is one library that does this.
Comment #7 by dlang-bugzilla — 2013-03-14T17:50:13Z
(In reply to comment #5) > I mean that if you've installed main() in a library file, it won't get pulled > in unless you have a reference to main somewhere else, which is awfully > unlikely and I've never seen it. Doesn't the C runtime have a reference to main()?
Comment #8 by bugzilla — 2013-03-14T18:01:12Z
(In reply to comment #7) > Doesn't the C runtime have a reference to main()? No.
Comment #9 by bugzilla — 2013-03-14T18:04:34Z
(In reply to comment #6) > Some libraries define main(), and let the user define their own entry point. Even if there are some, the workaround is pretty easy - just add a module, any module, in .o form to the command line.
Comment #10 by dlang-bugzilla — 2013-03-14T18:04:56Z
Oh, sorry, I meant for C programs. main() isn't the entry point, not on Windows at least, so there has to be some initialization code that invokes it. For D, it's in Druntime, and it uses the C main as an "entry point". On Windows the C runtime defines WinMain, which initializes and calls the C main().
Comment #11 by hsteoh — 2013-03-15T10:27:59Z
Can't we just make the DMD-injected empty main() a weak symbol, so that it gets overridden if main is already declared anywhere else?
Comment #12 by bugzilla — 2013-03-15T13:57:22Z
You guyz make a good case. I amend my proposal so this should happen only when a "-main" switch is thrown on the command line.
Comment #13 by jens.k.mueller — 2013-03-15T15:09:46Z
Why is rdmd --main not good enough? I find this enhancement adds very little value.
Comment #14 by andrej.mitrovich — 2013-03-15T15:14:22Z
(In reply to comment #12) > You guyz make a good case. I amend my proposal so this should happen only when > a "-main" switch is thrown on the command line. Ok good. One other question: If another main() is found while -main is used, should the compiler error or just avoid generating main() on its own?
Comment #15 by andrej.mitrovich — 2013-03-15T16:52:19Z
(In reply to comment #14) > (In reply to comment #12) > > You guyz make a good case. I amend my proposal so this should happen only when > > a "-main" switch is thrown on the command line. > > Ok good. One other question: If another main() is found while -main is used, > should the compiler error or just avoid generating main() on its own? Disregard this question, there's already a check in DMD for multiple main functions, it will be very useful.
Comment #16 by andrej.mitrovich — 2013-03-15T17:53:21Z
Comment #17 by andrej.mitrovich — 2013-03-15T21:28:21Z
I'll let someone else work on this.
Comment #18 by bugzilla — 2013-03-16T00:55:55Z
Comment #19 by github-bugzilla — 2013-03-16T15:18:11Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/1eb44f54ac12ff747ab447be06e1fe430ffe77a9 fix Issue 9723 - add missing main() when compiling exe with -unittest https://github.com/D-Programming-Language/dmd/commit/a8fd11928e436d63936542576527759b488b3f4f Merge pull request #1753 from WalterBright/fix9723 fix Issue 9723 - add missing main() when compiling exe with -unittest
Comment #20 by andrej.mitrovich — 2013-04-12T10:08:29Z
Renamed title for changelog.