Author Topic: Mystery of the executable size  (Read 8548 times)

Offline Krice

  • Almost regular
  • **
  • Posts: 150
Mystery of the executable size
« on: July 02, 2023, 07:15:25 am »
Almost exactly ten years ago I released the last version of Brick Atelier (a pixel art program). I compiled it with C::B and the size of .exe back then was 232Kb. Now ten years later and about 10K more lines added to the project I'm trying to compile it again, but something is different. When I compiled without static linking the size of .exe is 282Kb (by the way, the old version was 32bit and this new one is 64bit). However it wont run, because it requires libgcc_s_seh-1.dll, libstdc++-6.dll and libwinpthread-1.dll. You can add first two dlls with -static-libgcc and -static-libstdc++ flags. However static linking was not needed in the old version and no dlls were needed either.

When you statically link with those the .exe size is now 1027Kb so considerably larger than before, even with 32 to 64 bit difference. And I'm still missing that thread library, which, if someone knows how to statically link it can give me some advice how to do it. I'm now wondering how the linking worked in the old version 10 years ago? It must have been statically linked by default, right? And why the size difference, because the program is more or less same, with new features sure, but it's not that different.

Offline nenin

  • Almost regular
  • **
  • Posts: 212
Re: Mystery of the executable size
« Reply #1 on: July 03, 2023, 07:34:51 am »
And I'm still missing that thread library, which, if someone knows how to statically link it can give me some advice how to do it.
You can try "-static", it should link all "standard" stuff statically.

Offline Krice

  • Almost regular
  • **
  • Posts: 150
Re: Mystery of the executable size
« Reply #2 on: July 03, 2023, 09:24:49 am »
-static results to error messages, I guess it's trying to statically link SDL2 library also, which seems to fail.

Offline nenin

  • Almost regular
  • **
  • Posts: 212
Re: Mystery of the executable size
« Reply #3 on: July 03, 2023, 08:32:29 pm »
-static results to error messages, I guess it's trying to statically link SDL2 library also, which seems to fail.
It should not be like this. You can try explicitly add SDL2 .dll, not .a, to list of the required libraries and test like this.
In general I have observations that for the new gcc lib should have name like *.dll.a to link dll. However, gcc can link dll directly.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Mystery of the executable size
« Reply #4 on: July 04, 2023, 03:02:52 am »
Quote
When you statically link with those the .exe size is now 1027Kb so considerably larger than before, even with 32 to 64 bit difference. And I'm still missing that thread library, which, if someone knows how to statically link it can give me some advice how to do it.

If I remember correctly, the libwinpthread-1.dll (thread library) is only allowed to be linked dynamically. I guess you are using mingw64 based gcc? maybe the gcc inside msys2/mingw64.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline nenin

  • Almost regular
  • **
  • Posts: 212
Re: Mystery of the executable size
« Reply #5 on: July 04, 2023, 08:31:15 am »
If I remember correctly, the libwinpthread-1.dll (thread library) is only allowed to be linked dynamically. I guess you are using mingw64 based gcc? maybe the gcc inside msys2/mingw64.
It is correct, but if standard libs are linked statically and project does not require pthreads libwinpthread-1.dll is not necessary and should not appears in dependencies.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Mystery of the executable size
« Reply #6 on: July 04, 2023, 09:36:17 am »
If I remember correctly, the libwinpthread-1.dll (thread library) is only allowed to be linked dynamically. I guess you are using mingw64 based gcc? maybe the gcc inside msys2/mingw64.
It is correct, but if standard libs are linked statically and project does not require pthreads libwinpthread-1.dll is not necessary and should not appears in dependencies.

In my memory, I think lib winpthread is a hard dependency that you can't remove. You can ask mingw64 devs for further question.  :)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Mystery of the executable size
« Reply #7 on: July 04, 2023, 05:13:06 pm »
In my memory, I think lib winpthread is a hard dependency that you can't remove. You can ask mingw64 devs for further question.  :)

It is a hard requirement when building GCC when several libs are built.

The OP might try the new MINGW GCC using non posix threads and see if the exe is smaller.

Edit: Add link https://winlibs.com/ See the ones with MCF threads

Tim S.

« Last Edit: July 04, 2023, 05:23:28 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline nenin

  • Almost regular
  • **
  • Posts: 212
Re: Mystery of the executable size
« Reply #8 on: July 04, 2023, 05:19:45 pm »
In my memory, I think lib winpthread is a hard dependency that you can't remove. You can ask mingw64 devs for further question.  :)
Standard libs are under authority of the GNU, not MinGW64-w. It is why winpthread appeared actually.
Now it looks like this (see attach)

Offline nenin

  • Almost regular
  • **
  • Posts: 212
Re: Mystery of the executable size
« Reply #9 on: July 06, 2023, 07:31:43 am »
The OP might try the new MINGW GCC using non posix threads and see if the exe is smaller.
exe size is not pumped by thread libs, actually they all are compact.  I suppose bloating  is the result of the implementation of the C++ std functionality.  Even with dynamic linking a lot of stuff is in-lined and generated a solid footprint.

Offline Krice

  • Almost regular
  • **
  • Posts: 150
Re: Mystery of the executable size
« Reply #10 on: July 16, 2023, 12:13:20 pm »
I ended up compiling with MSVC which had its own issues, but the resulted .exe is 250Kb. I was a bit uncomfortable with GCC license problems, although I don't think they are after some obscure freeware software. MSVC build obviously requires .dll files, but I think the redistributable package is usually already installed anyways. I quite dislike the legal text of GCC or GPL license, because it's not easy to even understand what you can actually do.

Offline nenin

  • Almost regular
  • **
  • Posts: 212
Re: Mystery of the executable size
« Reply #11 on: July 20, 2023, 10:45:37 am »
I quite dislike the legal text of GCC or GPL license, because it's not easy to even understand what you can actually do.
You can try clang, which can be found in some versions of WinLibs. clang has different license, as far as I know. However situation with exe size/dll`s will be the same.