Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,941 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,719 people online right now. Registration is fast and FREE... Join Now!




Size of executables compiled with gcc in Windows or Linux

 
Reply to this topicStart new topic

Size of executables compiled with gcc in Windows or Linux

joske
19 May, 2008 - 11:09 AM
Post #1

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
I started programming cross-platform using the gcc compiler. Now I'm wondering why there are such big differences in for example the size of the executables: For example when I compile the simple program posted below, in windows I end up with an executable of about 464kB, whilst in Linux it is only 9kB. This has to do with the include #include <iostream.h>. Same when using wxWidgets: in Windows you get an executable of about 3 MB, and in linux it is in the order of 300 - 500 kB's, dependent on the content of your program.

What is causing this difference? And why is it this way?

CODE
//
// Program to convert temperature from Celsius degree
// units into Fahrenheit degree units:
// Fahrenheit = Celsius * (212 - 32)/100 + 32
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
    // enter the temperature in Celsius
    int celsius;
    cout << "Enter the temperature in Celsius:";
    cin >> celsius;
    // calculate conversion factor for Celsius
    // to Fahrenheit
    int factor;
    factor = 212 - 32;
    // use conversion factor to convert Celsius
    // into Fahrenheit values
    int fahrenheit;
    fahrenheit = factor * celsius/100 + 32;
    // output the results (followed by a NewLine)
    cout << "Fahrenheit value is: ";
    cout << fahrenheit << " F" << endl;
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    system("PAUSE");
    return 0;
}

User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Size Of Executables Compiled With Gcc In Windows Or Linux
19 May, 2008 - 11:29 AM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
There are a lot of things that affect the executable size.

One of the main things, deals with how code is actually linked to you program. For example, take wxWidgets. In Windows, wxWidgets is not common, so people tend to actually just add the entire wxWidgets library to you exe. As I'm sure you can imagine, this is a fairly large library. You can also link to code through .dlls, but you will still have to include it with the exe. On Linux, wxWidgets is a lot more common, so it may actually be inlcuded with your distro. As such, when you actually compile the code, it is probably using a shared archive (which is basically Linux's version of a .dll), which is more then likely somewhere in your root, system, or similar directory, so you don't actually have to include it with your program.

This also extends to other libraries, such as the standard libraries.

Other then that, there is always optimizations. Turning on optimizations (-O2 for GNU) can generate smaller programs, but that really isn't a issue with the code you posted.

The "bloat" you are seeing is just dealing with how you link libraries into your exe.

What compiler are you using on Windows?
User is offlineProfile CardPM
+Quote Post

joske
RE: Size Of Executables Compiled With Gcc In Windows Or Linux
20 May, 2008 - 01:12 AM
Post #3

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
Thanks for the explanation, Cerolobo. So on Linux, the compiler smartly re-uses existing libraries, which keeps the program small. Is this library-usage also the reason that you have to compile the executable separately for each different linux distribution? Because the program links to libraries that can be different on each platform?

And secondly: So, if I understand it well, gcc is not optimized for reusing libraries in Windows. For example VC++ seems to compile programs on Windows more efficiently as compiling the program I posted above with VC++ results in an executable of a about 30kB - 40kB.

About the compiler: I'm pretty new to cross-platform programming. I'm playing around with wxDev-C++, CodeBlocks, and kDevelop. But so far I prefer using a colored text-editor and a command line most. I have a simple bash script which I run, see attachments.



Attached File(s)
Attached File  compile.sh.txt ( 400bytes ) Number of downloads: 24
Attached File  compile.bat.txt ( 302bytes ) Number of downloads: 25
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Size Of Executables Compiled With Gcc In Windows Or Linux
20 May, 2008 - 02:44 AM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
QUOTE(joske @ 20 May, 2008 - 02:12 AM) *
Is this library-usage also the reason that you have to compile the executable separately for each different linux distribution? Because the program links to libraries that can be different on each platform?

No, not really. When you actually run a program, the program will attempt to locate and load the required libraries into memory. If they aren't found, then the program just won't run.

As long as the library has the correct name, the program should be able to locate it. Shared libraries actually have several benefits. Namely, only one copy of the library has to be loaded into memory at one time (IE, several programs can access and use the same library without loading it more then once). You can also provide updates for various programs, by just updating the shared library; although, this can lead to version conflicts.

Compiling programs from the source level provides a few benefits. The main one deal with optimizations. Take SSE for example. If your processor supports said extension, then telling the compiler to use that extension will generally provide better performance, and smaller code. That's why a lot of Linux projects have "configure" scripts (which I hate for various reason...).

One of the nice things about stuff like apt-get is that they do tend to download any dependencies the program may need.


QUOTE(joske @ 20 May, 2008 - 02:12 AM) *
And secondly: So, if I understand it well, gcc is not optimized for reusing libraries in Windows. For example VC++ seems to compile programs on Windows more efficiently as compiling the program I posted above with VC++ results in an executable of a about 30kB - 40kB.

About the compiler: I'm pretty new to cross-platform programming. I'm playing around with wxDev-C++, CodeBlocks, and kDevelop. But so far I prefer using a colored text-editor and a command line most. I have a simple bash script which I run, see attachments.

It really depends on the compiler. There are two main Windows GCC ports that I know of, cygwin and MinGW. Since you mentioned Dev-C++, and you have it in your script, I assume you are using the MinGW port (which is what Dev-C++ comes with).

In most of my experiences, MinGW actually producers smaller executables then the MS compiler.

Referring to the script, you use *.cpp (which I do as well, and I love it to death). While this is very convenient, it can lead to unnecessary code bloat. For example, take the program in your first post. If you had additional libraries in the directory that code is located in, yet you don't use any of the other code, it will still be compiled into your program along with what it those libraries include.

You can further reduce the size of the executables, if you remove unnecessary includes. Take your first post again.

#include <cstdio>

is doing absolutely nothing, since you don't call any functions from it.

You also have

#include <cstdlib>

for system(). Personally, I frown on the use of system(). While the function is actually a part of the C standard, the results are not portable.

So, with a little revising, you could have this

CODE
//
// Program to convert temperature from Celsius degree
// units into Fahrenheit degree units:
// Fahrenheit = Celsius * (212 - 32)/100 + 32
//
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
    // enter the temperature in Celsius
    int celsius;
    cout << "Enter the temperature in Celsius:";
    cin >> celsius;
    // calculate conversion factor for Celsius
    // to Fahrenheit
    int factor;
    factor = 212 - 32;
    // use conversion factor to convert Celsius
    // into Fahrenheit values
    int fahrenheit;
    fahrenheit = factor * celsius/100 + 32;
    // output the results (followed by a NewLine)
    cout << "Fahrenheit value is: ";
    cout << fahrenheit << " F" << endl;
    return 0;
}


If you absolutely must have the pause at the end, check out this thread.
http://www.dreamincode.net/forums/showtopic30581.htm

Now, stdio.h and stdlib.h should not add any additional code, but other headers may do so.

To actually compare the results of the iostream implementation, here are some results with the slightly modified C++ version above, as well as a pure C version

Pure C Version
CODE
#include <stdio.h>

int main(void)
{
  int celsius;
  int fahrenheit;

  printf("Enter the temperature in Celsius:");
  scanf("%d", &celsius);
  fahrenheit = (212 - 32) * celsius/100 + 32;
  printf("Fahrenheit value is: %d F\n", fahrenheit);

  return 0;
}


Results
CODE
Cygwin GCC Port
  g ++ -Wall -Wextra -ansi -pedantic
    C++ - 477,718 bytes
    C   -   9,524 bytes
  g ++ -Wall -Wextra -ansi -pedantic -O2
    C++ - 477,562 bytes
    C   -   9,012 bytes
    
    The following compile with -mno-cygwin, which basically makes it MinGW
    
  g ++ -Wall -Wextra -ansi -pedantic -mno-cygwin
    C++ - 475,377 bytes
    C   -  12,410 bytes
  g ++ -Wall -Wextra -ansi -pedantic -mno-cygwin -O2
    C++ - 474,724 bytes
    C   -  12,410 bytes
    
MS Complier v 15 (Visual Studio 2008 Pro)
  cl /W3 /EHsc
    C++ - 146,432 bytes
    C   -  55,808 bytes
  cl /W3 /EHsc /O2
    C++ - 144,384 bytes
    C   -  55,808 bytes
    
Borland Compiler
  bcc
    C++ - 148,992 bytes
    C   -  66,048 bytes


So, in terms of file size, you can conclude that the GCC implementation of iostream is far more bloated then the MS or Borland version. I did check the files the GCC (with -mno-cgywin) and the MS version. Both of them only depended on standard Windows DLLs, yet the MS version still depended on fewer dlls (MS - Kernel32.dll GCC - Kernel32.dll and MSVCRT.dll). This basically just means that yes, the GCC implementation of iostream isn't that great.

If file size is important, you may want to switch to C.
User is offlineProfile CardPM
+Quote Post

joske
RE: Size Of Executables Compiled With Gcc In Windows Or Linux
20 May, 2008 - 03:57 AM
Post #5

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
Thanks for the detailed explanation and your tips. As for the iostream: since I discovered that Windows executables become such big when using iostream, I just avoid it and use printf and scanf etc. smile.gif.

Of course the size of an executable is not dramatically important - performance is more important. I just don't like programs being multiple MB's when I know it can easily be reduced to a couple of hundreds of kB's when optimized. Such a waste of hard disk space and of internet-traffic...

This post has been edited by joske: 20 May, 2008 - 03:58 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:40AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month