AwesomeCronk
AwesomeCronk

Reputation: 481

Why do different compilers produce different sized files?

I recently installed MinGW on Windows 10 with the packages for C and C++. The other day, I decided to compile some C++ using Visual Studio 1029's compiler and g++ to see if there was a difference. There was a major difference in that the file produced by g++ is over four times larger than the file produced by VS19.

Here's what I did: I first opened Visual Studio and created a Visual C++ Console Application named Test1. I then placed the following code in main.cpp:

// Test1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

int main()
{
    std::cout << "Hello World!";
}

Then I went to the build menu, configured VS19 to release mode, and built the project. Then I opened the solution folder, navigated to the Release folder and copied the .exe file. I then created a folder elsewhere, named sizes (any name should work), and pasted the .exe file there, under the name VS.exe. I then went back to the solution folder for Visual Studio, navigated to the Test1 directory, copied the Test1.cpp file, and pasted it in my sizes folder as main.cpp. I then opened PowerShell in the sizes folder and ran the following commands:

PS [*****] C:\Users\*****\Documents\C_C++\Sizes> g++ -o mingwgpp.exe main.cpp
PS [*****] C:\Users\*****\Documents\C_C++\Sizes> gci

    Directory: C:\Users\*****\Documents\C_C++\Sizes

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        5/12/2020     15:01                .vs
-a----        5/12/2020     14:13            173 main.cpp
-a----        5/12/2020     15:01          47192 mingwgpp.exe
-a----        5/12/2020     14:14          10752 vs.exe
PS [*****] C:\Users\*****\Documents\C_C++\Sizes> ./vs.exe
Hello World!
PS [*****] C:\Users\*****\Documents\C_C++\Sizes> ./mingwgpp.exe
Hello World!
PS [*****] C:\Users\*****\Documents\C_C++\Sizes>

Interestingly, even though they are both built from the same source code, the executable built through Visual Studio is far more compact. Why is this?

Upvotes: 2

Views: 349

Answers (1)

UweJ
UweJ

Reputation: 489

You can assume each compiler as a different software to create machine-runnable code from Files containing code which is programmed according to a specific standard, e.g. C++. Each of these compilers have their specific skills/features. A good compiler optimizes the code. This is also the reason why code compiled in debug mode is mostly much slower than the release compiled code.

To have a look on it, try https://godbolt.org/ There you can choose the compiler and can analyse the output.

Upvotes: 3

Related Questions