Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For identical c++ source file, its gcc executable is 655 times bigger in Windows than in Linux. Why this much difference?

I was testing this simple c++ code on my Windows 8.1, Intel i7-3517U 64 bit laptop with GCC 4.8.2.

#include<iostream>

using namespace std; 

int main(int argc, char **argv){
   cout << "This test code will simply display any arguments passed." << endl ;
   for(int i=0; i<argc; i++){
      cout << argv[i] << endl ;
   }
   return 0 ;
}

Surprisingly, after compiling, the executable turned out to be of 5905KB. Out of curiosity, i tried compiling the same file with the same GCC version on Linux Fedora 20 64 bit machine. And the executable was just 9KB.

After doing various optimization using g++ -Ox -o fileWithOx.exe file.cpp (x=1,2,3,s), Windows executable were nearly same in size. After doing some research, and following MinGW's advise i tried compiling them without debugging information using strip g++ -s -o fileWithStrip.exe file.cpp, but the executable was still 597KB large.

While on the other hand, Linux executable were of just 6-13KB for same options.

Doing some experiments, some more research & stack overflowing, i was nearly convinced that the gigantic size is due to iostream linking to many other header files and/or generating some initialization code.

But my doubt is that iostream is used both in Windows as well as in Linux. Then why so much difference in size? I know that Windows and Linux executable work differently. But 655 times bigger, isn't this a bit extreme?

like image 406
hardy Avatar asked Sep 21 '25 07:09

hardy


2 Answers

The difference is probably not due to compilation but to link-edition phase.

First try to compile only, stop the command using -c e.g. g++ -c code.cpp in Unix-like and find the equivalent flags in your windows environment. Then compare the object files, they should be almost the same size. These object files (.o extension) only contains the translation of your code to the machine-code. The size could be different, say a factor of 2 or 3 maybe but this is not relevant.

What happened is that your Windows's compiler probably used a static linking on some library, thus the executable file contains the code of what you use in the library. On Unix-like the compiler probably linked everything dynamically, which says that the library will be loaded at runtime and not included in the executable.

Please refer to your compilers documentation to find how to enforce static/dynamic linking. You alos should be aware that it may be possible that some environment may not provide both kind of libraries...

like image 77
Jean-Baptiste Yunès Avatar answered Sep 22 '25 21:09

Jean-Baptiste Yunès


Just built your program under windows, using mingw g++ version 4.7.2 - I only get 10kb. No idea how you managed to bloat it to over 5MB. Here's the cmd line and output when I hit Ctrl-F11 in Code::Blocks 13.12:

mingw32-g++.exe -Wall -fexceptions -O2 -I"C:\Program Files (x86)\CodeBlocks\MinGW\include" -c C:\Users\enhzflep\Documents\code\001-sizeTestDeleteMe\main.cpp -o obj\Release\main.o mingw32-g++.exe -o bin\Release\001-sizeTestDeleteMe.exe obj\Release\main.o -s
Output file is bin\Release\001-sizeTestDeleteMe.exe with size 10.50 KB

like image 28
enhzflep Avatar answered Sep 22 '25 22:09

enhzflep