minimize executable size

This is a discussion on minimize executable size within the C Programming forums, part of the General Programming Boards category; A simple "hello world" program in Microsoft Visual C++ takes up to 150 kb. How would it be possible to ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    28

    minimize executable size

    A simple "hello world" program in Microsoft Visual C++ takes up to 150 kb. How would it be possible to minimize the executable size without damaging portability? Thanks.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The two main ways, turn the debugging info off (i.e. compile a release build) and/or dynamically link libraries (which has its downsides).

    AFAIK, Visual C++ has optimisations for small executable sizes.
    Last edited by zacs7; 09-06-2009 at 04:40 AM. Reason: cough

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by zacs7 View Post
    ...dynamically link libraries (which has it's downsides).
    Besides the fact that the correct word is "its", static linking also has its downsides: bigger executable size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by heisel View Post
    A simple "hello world" program in Microsoft Visual C++ takes up to 150 kb..
    No it doesn't. You must be including irrelevant libraries, the debug build, and DLL linkage.

    Switch to the Multi-threaded static linkage.
    Switch to release version.

    Watch as your executable size shrinks to a few K
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    Quote Originally Posted by abachler View Post
    Switch to the Multi-threaded static linkage.
    Switch to release version.
    How? I have the 6.0 version.

    Quote Originally Posted by zacs7 View Post
    ...and/or dynamically link libraries (which has its downsides).
    You mean by LoadLibrary and GetAddressProc?

    Quote Originally Posted by zacs7 View Post
    AFAIK, Visual C++ has optimisations for small executable sizes.
    Such as?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by heisel View Post
    How? I have the 6.0 version.


    You mean by LoadLibrary and GetAddressProc?


    Such as?
    Project Settings/ C++ / Category - Code Generation / Use Run-Time Library

    to choose static or dynamic linkage to CRT

    Category - Optimization - choose Minimize Size

    To select Release build - select Release on the Build toolbar
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Consider upgrading to newest Visual Studio if you can. The express version is free.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    Quote Originally Posted by vart View Post
    Project Settings/ C++ / Category - Code Generation / Use Run-Time Library

    to choose static or dynamic linkage to CRT

    Category - Optimization - choose Minimize Size

    To select Release build - select Release on the Build toolbar
    I managed to decrease it to 28 kb, in this way. In Optimization category, though, optimizations list is greyed out.

    Quote Originally Posted by Elysia View Post
    Consider upgrading to newest Visual Studio if you can. The express version is free.
    I am stuck with the this version. Maybe, I will consider it some day.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Code:
    #include <stdio.h>
    int main()
    {
            printf("Hello World!\n") ;
            return 0;
    }
    Compiles to a 4719 byte binary in Linux, but I'm sure there's a good reason about the difference in file sizes from Windows the Linux. I reckon it has to with the libraries.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    I reckon it also has something to do with that the OP is using a very old version.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Use the rights flags , size optimization, the rights #pragma and its size will be < 1 Kb...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 05:14 PM
  2. Pointer Size Relative To Integers?
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 04-18-2006, 06:58 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21