Smallest executable

closed account (S6k9GNh0)
I need an extremely optimized compiler that gives a very small output file. I'm limited extremely on space where I'm working on my projects currently. A simple executable like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <sstream>

int main(int argc, char ** argv)
{    
    std::cout << "Please enter the grade you recieved in programming: ";
    
    int grade;
    std::string input;
    
    while(true)
    {
        getline(std::cin,input); //Recieve grade from user.
        std::stringstream temp(input);
        if (temp >> grade)
           {
                 if (grade >= 90 && grade <= 100)
                           std::cout << "Congratulations! You made an A!";
                 else if (grade >= 80 && grade < 90)
                      std::cout << "Congratulations! Your above average with a B!";
                 else if (grade >= 70 && grade < 80)
                      std::cout << "Boo! Your an average Joe with a C!";
                 else if (grade >= 60 && grade < 70)
                      std::cout << "Boo! You fail at life with a D!";
                 else if (grade >= 0 && grade < 60)
                      std::cout << "Failure! Try better slob! F!";
                 else { std::cout << "Please enter a valid grading number: "; continue; }
                 std::cout << std::endl << grade;
                 break;
           }
        else std::cout << "Invalid number, please try again: ";
    }
    
    getchar();
}


...compiles to a little bit over a half megabyte. In C, the executable would be around 50 KB. I've tried MinGW but it's known for giving a big as hell executable. I'm trying as hard as I can to avoid VC++. Borland C++ is outdated. And I can't use the DM ++ compiler on school premises although that hasn't stopped me before. Any suggestions? I wish TinyC supported C++. T.T
Last edited on
compiles to a little bit over a half megabyte. In C, the executable would be around 50 KB

http://www.research.att.com/~bs/bs_faq.html#Hello-world

Which optimization level are you using?
What's wrong with VC++? Why would you avoid it on purpose? Anyway, doesn't any good compiler provide optimization settings? The moment you start using things from the stream libraries your executable grows. I'm sorry to say that I do not believe that you can shrink a c++ program like that down to 50KB.

You'd have to rewrite the program to shrink it down. Manually parse the strings and use character arrays instead of std::string. In other words, write a c program and the executable will be much smaller. That's the tradeoff between C and C++ I'm afraid.
closed account (S6k9GNh0)
There's lots of problems with VC++ that I don't like. One, I've limited time, it's not very portable, and it's not platform flexible.

Are streams the real reason why C++ is so huge >.<'? I never really push the effort to figure out why until I needed it the most.

All of MinGW's optimizations make no difference.
The executable file of that program is 9.7 kB on my computer using g++ 4.3 with -O3 -Os -s
( 14.3 kB with no optimization )
Last edited on
I can't get it below 250 KiB with MinGW 3.x (4.x gives even bigger executables).
VC++ can get it to 11 KiB with dynamic linking, and to ~150 KiB with static linking.
Doesn't MinGW statically link the C++ runtime library to the executable?

This would explain the difference. Bazzy's EXE is dynamically linking the library.
Yes, it does. I think Bazzy must be using GCC.
I couldn't get it below 316kb myself using the integrity compiler I have. I don't see any such option for dynamic vs static linking of run time libraries. I have to email tech support for this compiler to find out why.
Last edited on
Due to GPL issues, MinGW makes it difficult to dynamically link to libstdc++.

Given:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
  {
  cout << "Hello, world!\n";
  return 0;
  }


Static linking: 488,000 bytes
Dynamic linking: 5,120 bytes
  libgcc_s_1.dll: 44,032 bytes
  libstdc++_6.dll: 772,096 bytes
  total: 821,248 bytes

Use static linking.

Hope this helps.
> I'm trying as hard as I can to avoid VC++.

Nonsense.
With VC++, you can do an exe < 0.5 Kb in C...
Last edited on
With VC++, you can compile the Linux kernel and make it fit in your MBR. Boot loaders? Fuck that!
closed account (S6k9GNh0)
LOL @ ^

And VC++ isn't flexible. That's not nonsense, that's the truth. Plus it's proprietary and commercialized which I'm heavily against...
Last edited on
Topic archived. No new replies allowed.