Thread: Creating small executables

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    Creating small executables

    Hi all,

    Im using Visual Studio 6 for creating my win32 apps however no matter how i code my project it always produces and exe of about 500k. I thought c++ was a low(ish) level language capable of producing small exe's? I mean the projects im coding are relatively small, no more than about 100 lines of code. So why are the exe's so big? Is it down to the actual compiler or is it something im doing wrong? If its me then what are the techniques for producing small code and if its the compiler are there any options which will help me?

    Thanks

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You can turn on optimizations in the compiler to make a smaller exe file. Most likely you are doing a debug build that includes debugging symbols therefore making your exe bigger.
    Woop?

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>Is it down to the actual compiler

    Yes MS Visual C++ 6.0 is absurd for executables - the same
    program compiled under Dev-Cpp will typically be much smaller.
    You can help it though by thoroughly reading the documentation
    and optimising the compiler more - I just compiled hello world on
    that compiler this minute - 240kb!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    wow 240k, thats completely ridiculous. I might have a look at another compiler and see what I think. Im trying to get away from Microsoft products as they are always bloated with useless junk. I'll give that dev-cpp ago.

    Thanks for the reply both

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Dev-C++ uses the MinGW port of GCC, and due to both technical and legal limitations the executable size may still be larger than you expect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    Quote Originally Posted by laserlight
    Dev-C++ uses the MinGW port of GCC, and due to both technical and legal limitations the executable size may still be larger than you expect.
    Can u please tell me what are those 'legal' limitations that prevent the executable size to be small?

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    Why is my C++ binary so large?

    C++ programs using the Standard Template Library (ie/ #include <iostream>) cause a large part of the library to be statically linked into the binary. The need to statically link the stdc++ into the binary is two fold. First MSVCRT.dll does not contain C++ stdlib constructs. Second the legal implications of generating a libstdc++.dll are restricted by the licensing associated with the library. If you wish to keep your file size down use strip to remove debugging information and other verbatim found in the binary.

    strip --strip-all SOMEBINARY.exe


    ---
    copied from that link laserlight provided

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    34
    Hmm, under visual c++ 2005, a "hello world" console app using c++'s <iostream> library, and all default settings, creates a 7.5k exe file.

    I'd suggest using libctiny though....check out this article:

    http://msdn.microsoft.com/msdnmag/issues/01/01/hood/

    all you have to do is add libctiny.lib to the linkers's additional dependencies, and you end up with a small exe. I've personally used it on both console and gui windows apps (straight win32, no MFC or anything), and it works great, although there are times when I want to use a certain function and it ends up not working anymore.

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by veecee
    Hmm, under visual c++ 2005, a "hello world" console app using c++'s <iostream> library, and all default settings, creates a 7.5k exe file.
    VC 2005 by default links dynamically so natuarally the builds will be smaller. To do a fair comparison change the "runtime-library" setting to multi-threaded instead of multi-threaded dll.

    Starting with default setting from an empty project, I get exe of 108k
    setting optimizations for smaller code only brought it down to 92k

    Switching to printf("Hello World"); reduced it to 46k

    To be honest though, it's really silly to judge size based on hello world. Real programs are typically going to use more of the linked in items and thus be more efficient size-wise. For example I just added to my hello world program a for loop that pushed 1000 ints into a vector then output them to the screen. My size jumped from 108 to 117. Almost nothing compared to the initial byte cost.
    Last edited by Darryl; 05-25-2006 at 01:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  3. Small executables in VC++ 8
    By Bleech in forum Windows Programming
    Replies: 3
    Last Post: 06-20-2007, 08:28 AM
  4. STL + MSVC++ = big executables ?
    By teneniel in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 02:12 PM
  5. creating a small database need some hints
    By asim0s in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2002, 10:44 AM