www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9723] New: add missing main() when compiling exe with -unittest

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723

           Summary: add missing main() when compiling exe with -unittest
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bugzilla digitalmars.com


--- Comment #0 from Walter Bright <bugzilla digitalmars.com> 2013-03-14
14:32:08 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 14 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-03-14
14:55:25 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #2 from Walter Bright <bugzilla digitalmars.com> 2013-03-14
16:59:40 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-03-14
17:02:11 PDT ---
(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? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #4 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-03-14
17:03:12 PDT ---
(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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #5 from Walter Bright <bugzilla digitalmars.com> 2013-03-14
17:39:32 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723


Vladimir Panteleev <thecybershadow gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thecybershadow gmail.com


--- Comment #6 from Vladimir Panteleev <thecybershadow gmail.com> 2013-03-15
02:49:04 EET ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #7 from Vladimir Panteleev <thecybershadow gmail.com> 2013-03-15
02:50:13 EET ---
(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()? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #8 from Walter Bright <bugzilla digitalmars.com> 2013-03-14
18:01:12 PDT ---
(In reply to comment #7)
 Doesn't the C runtime have a reference to main()?

No. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #9 from Walter Bright <bugzilla digitalmars.com> 2013-03-14
18:04:34 PDT ---
(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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #10 from Vladimir Panteleev <thecybershadow gmail.com> 2013-03-15
03:04:56 EET ---
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().

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723


hsteoh quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh quickfur.ath.cx


--- Comment #11 from hsteoh quickfur.ath.cx 2013-03-15 10:27:59 PDT ---
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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #12 from Walter Bright <bugzilla digitalmars.com> 2013-03-15
13:57:22 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723


jens.k.mueller gmx.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jens.k.mueller gmx.de


--- Comment #13 from jens.k.mueller gmx.de 2013-03-15 15:09:46 PDT ---
Why is rdmd --main not good enough? I find this enhancement adds very little
value.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #14 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-03-15
15:14:22 PDT ---
(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? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #15 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-03-15
16:52:19 PDT ---
(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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #16 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-03-15
17:53:21 PDT ---
https://github.com/D-Programming-Language/dmd/pull/1751

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|pull                        |
         AssignedTo|andrej.mitrovich gmail.com  |nobody puremagic.com


--- Comment #17 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-03-15
21:28:21 PDT ---
I'll let someone else work on this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #18 from Walter Bright <bugzilla digitalmars.com> 2013-03-16
00:55:55 PDT ---
https://github.com/D-Programming-Language/dmd/pull/1753

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723



--- Comment #19 from github-bugzilla puremagic.com 2013-03-16 15:18:11 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 16 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9723


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|add missing main() when     |Implement -main switch to
                   |compiling exe with          |inject a default main()
                   |-unittest                   |function


--- Comment #20 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-04-12
10:08:29 PDT ---
Renamed title for changelog.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 12 2013