Re: MinGW generates large executable files

Keith Marshall <[email protected]> Mon, 23 Mar 2020 20:12:20 +0000
Newsgroups gmane.comp.gnu.mingw.user
Organization MinGW.org Project
Message-ID <[email protected]>
On 20/03/2020 22:15, Charles Bailey wrote:
> I have been trying to determine why the executable files produced by 
> MinGW are much larger than I would expect, even for small, simple, 
> console-mode programs.  I tried an experiment with what is probably
> the smallest possible C program. Just one line:>
> int main(void) {return 0;}

For me, cross-compiling this with mingw32-gcc-9.2.0, and the upcoming
(yet-to-be-published) mingwrt-5.3.1, yields a 20480 byte executable:

   $ echo 'int main(void){return 0;}' | mingw32-gcc -s -xc -
   $ ls -l a.exe
   -rwxr-xr-x 1 keith keith 20480 Mar 23 18:49 a.exe

> When I compile this with TCC I get a .exe file that is just 1,536 
> bytes.  With gcc from MinGW I get a .exe that is 42,035 bytes long. 
> Executing the strip command on it to remove debug symbols brings it
> down to 19,470 bytes, which is still more than 12 times the size of
> the TCC-produced file.
So, comparable to my experience, then; (with mingw32-gcc-8.2.0, and
mingwrt-5.2.3, I see 19456 bytes).

> [...snip...]
> 
> What is all that extra stuff that MinGW is inserting?  Why is it
> referencing tons of functions that I'm not even using?

Eli has already answered this, but some further explanation may be merited:

 * The DLL imports, on which you seem to be focused, actually
   have a negligible effect on executable size; the dominant
   contribution is from MinGW's more comprehensive, more robust,
   and predominantly statically linked run-time startup -- and
   perhaps to a lesser extent, shutdown -- code.

 * The size of the executable may be reduced, at the cost of
   additional DLL dependencies, by _more_ dynamic linking; for
   example, libmingwex may be dynamically linked, (supported
   since mingwrt-5.0, I have libmingwex.dll.a in ./mingwrt/):

      $ echo 'int main(void){return 0;}' |
          mingw32-gcc -s -xc - -L ./mingwrt/
      $ ls -l a.exe
      -rwxr-xr-x 1 keith keith 11776 Mar 23 19:14 a.exe

> Are there some gcc options I can specify to eliminate that extra
> stuff?
Yes, but using them is far from trivial: consult GCC documentation for
-nostdlib, and related options, but note that if you use them, you will
likely need to write your own startup code.

-- 
Regards,
Keith.

Public key available from keys.gnupg.net
Key fingerprint: C19E C018 1547 DE50 E1D4 8F53 C0AD 36C6 347E 5A3F

_______________________________________________
MinGW-Users mailing list
[email protected]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.osdn.me/mailman/listinfo/mingw-users
Also: mailto:[email protected]?subject=unsubscribe
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEwZ7AGBVH3lDh1I9TwK02xjR+Wj8FAl55GCUACgkQwK02xjR+
Wj/CFg//STOj9ZzSki18b6NUr5lDkp89R1GRvTK1oSPiXnHyxJhrOfuYbN7+IoON
VPlq0MjvMKQDDx8hRPy7Wd8Eq5zrc8DG4N6iLEn4Kk3kToSI/+Fhl88MIlO0wAGb
K8wc9ZjoBkfCwVrVHovhb/lpuNhdsGZQ3WT9NZiOBIszPXIYSHSP4T+3T7xpp8ED
FwuFjKOtrxO+FT/UWrPvw/XX/X9pLNqYwrOZvc1Q3xhXvn2RDQSOzPYpoYxJOiFh
ELAXswQHBtWWfGlXl0TXJwTqKG9iFosWLEPE5GlwgPZoAWEckxwOedyVKlSgTBaK
oHXYWbx/YsC+KRM0Q0ewniFaAaLeGndusSnl+U4pqSc4DeYJr658aDoYPOsWcGFb
NR2czjApI7k6POZziY8CV5QMjgirS/uJFS/WUPClkYYqBAapTNu8O1XbnNlQ3TX5
FBy8rHmdlLuHjbRkcOfQTewNZFUWyxRhBMq0AITyMb/rGAluFFr3g+MIL7FHxsw+
qsFv6heNaVwHEz26mKR0/rW4OuvJiRuFri5ITaHGhDiBAjWSeZ9O95wkgzEGOuir
bLchGnhdSnZmK9eT1jQKZHcTgaMn4MOBFcPULhYvU31Q42J742Auu3piMrl+sOO1
zd9OaDs9ySuhY6m+uKwCzsiMQO/ZKYaFjgcKbs4IeoqBSO38ZOk=
=p9eD
-----END PGP SIGNATURE-----