Thread: Reducing the size of compiled file

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    27

    Reducing the size of compiled file

    Hi,

    As a learning curve i wrote a very simple console program. The compiled executable for this is about 460kb. It seems that anything from a few lines of code to 100 or more is still around 460kb in size.

    A friend of mine who uses Visual Studio tells me that there are two compile options, debug and release, the latter option greatly reducing the file size but does not include the behind the scenes debugging etc.

    I am using Bloodshed Dev C++ 4.9.9.2, does anyone know if it is possible to achieve a similar reduction in file size with this compiler?

    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you could strip the executable using strip or by passing -s to g++ (the default compiler for Dev-C++).

    Besides that, you could look into optimising by passing -O1, -O2 or -Os, the latter being specifically intended to optimise for size.

    You could also try reducing the size with UPX.
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Man, this really needs to be in the FAQ
    Try a board search for "executable size"
    Lots to read
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Thank you


    Quote Originally Posted by Salem
    Try a board search for "executable size"
    ... and sorry, should have tried the search first :\

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Why is it that so many people are concerned over whether their program is 500kb or 200kb? Or are they all just embedded programmers and I don't know it?

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    For me it's simply a case of "out of interest", rather then it being an important issue. I couldn't understand why the executable was nearly half a meg in size when there was a relatively small amount of code.

    iostream seems to be responsible judging by comments in threads returned from the search. I am wondering if there is a way to use only the things i need from the include file, for example i recall seeing something like:

    using std::cout, std::cin, std::endl;

    Presuming that i correct can i use that in place of:

    using namespace std;

    Or will that have no effect of the file size?

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Kudos for your curiosity.

    What #include does is just copy and paste the entirety of another file into your source code. So restricting your namespace usage won't affect size because all of <iostream> will still be included in your code whether you use it or not.

    I've heard of some people using pre-processor directives to prevent certain things from being included in their program, but that would mean mucking with (a) the pre-processor and (b) standard header files.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Needless to say, anything like that is beyond me at the moment. I feel better knowing what's happening to cause the large file size.

    I figure it makes more sense to question everything now rather then getting a year down the line only to realise i don't understand the simple things.

    Thank you for the information.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Why is it that so many people are concerned...
    I think it's just a little shocking to find that 100 bytes of HelloWorld.CPP becomes 500,000 bytes of HelloWorld.EXE!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But after you've gotton over the initial hump at the start, it all settles down. You can add lots of code and the size of the excutable either doesn't change or just creeps up slowly in response to the increase in size of your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, including <iostream> throws a whole big heap of code into your program... just as an examle, iostream #includes at least <istream> and <ostream>, <istream> includes <ios> and <limits>, <ostream> just includes <ios>.

    <ios> has it's own list of includes, as follows:
    Code:
    #include <iosfwd>
    #include <exception>     // For ios_base::failure
    #include <bits/char_traits.h>   // For char_traits, streamoff, streamsize, fpos
    #include <cstdio>   // For SEEK_SET, SEEK_CUR, SEEK_END
    #include <bits/localefwd.h>  // For class locale
    #include <bits/ios_base.h>  // For ios_base declarations.
    #include <streambuf> 
    #include <bits/basic_ios.h>
    stopping at <ios> for the sake of sanity, I'll count how many new lines of code were just #included into mine (this will depend on your compiler's implementation):

    <limits> in itself is 1,143 lines long, <ios> is 53 lines long, <istream> is 774 lines long, <ostream> is 548 lines long, and <iostream> is 80 lines long.

    that makes at least 2,598 extra lines. that's not including all those other files that <ios> included, but I assure you it's thousands more than that.

    however, as I said before, pretending that <ios> is the end of the train, you can see that this 'hello world' application:
    Code:
    #include<iostream>
    
    int main()
    {
        std::cout<<"Hello World";
    }
    ends up being over 2,600 lines... way over.

    in other words, simply #including <iostream> brings in so many lines of code, that it inflates the size of your program in a way that you would have to write hundreds, maybe a thousand lines of code to see any significant change in the size of your code.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    however, as I said before, pretending that <ios> is the end of the train, you can see that this 'hello world' application:
    Code:
    #include<iostream>
    
    int main()
    {
        std::cout<<"Hello World";
    }
    ends up being over 2,600 lines... way over.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM