none
Weird ! My exe file size

    Question

  • Hi,

    Please try these steps with me:

    1.Make a new Windows Form Application.

    2. Build your project without changing any thing.

    3. Check the exe file of your project( locating in the Debug folder) and find its size. It will be around 50 kB.

    4. Edit the code of your form (Form1.h) by adding the following line in the begining of your file: #include <iostream> or #include<string>

    5. Rebuild your project

    6. Check again the size of your exe file. It will be around  1200 kB !!!

    Can anyone explain to me whats going on?

    I am using Microsoft Visual C++ 11 Beta, but I check the problem with version 10 also and the same thing happen.



    Wednesday, April 04, 2012 3:07 PM

Answers

  • OK, I found the cause for the large size of exe file but please don't ask why because I don't have an answer:

    THE SOLUTION:

    1. Go to your project property. 
    2. Select Configuration Properties -> General
    3. Under the Project Defaults find "Common Language Runtime Support" and change it from "/clr:pure" to "/clr"
    4. That's all. Rebuild your project and you find your exe file go back to a small size file again! port it to other OS like Vista without a problem.
    • Marked as answer by Ahmed Shakir Friday, April 13, 2012 4:18 AM
    Friday, April 13, 2012 4:18 AM

All replies

  • Hello Ahmed,

    please excuse. I'm VB.NET fan. The lenght of the  .exe in the same situation is 18,5 kb.

    When I import this namespaces no changes in lenght occurs.  (because not instanciated)

    regards Ellen


    Ich benutze/ I'm using VB2008 & VB2010

    Wednesday, April 04, 2012 7:22 PM
  • Include statements cause the referenced files to be "added" inline into your source code files.  Take a look at the iostream.h file.  That file has its' own #includes, and those files have their own #includes, etc.  All of those C++ source files are read and compiled as if they are part of your own small file.  

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Wednesday, April 04, 2012 11:49 PM
  • Hello Rudy,

    #include<iostream>

    using namespace std;

    int main(){

    cout<<"Hello World!"<<endl;

    return 0;

    }

    the exe file of this code which include the iostream is 65 kB !

    Now if you add more header files like <string> <complex> <windows.h> <stdio.h> etc the size will be the same 65 kB. The reason of that is simple: you didnt used them.
    Thursday, April 05, 2012 12:49 PM
  • The best that I can tell you is that the Compiler behavior is by design.  I have no doubt that if you spend the time to go through the code that you will run into conditional compilation code, ultimately probably due to the project's type. that causes more/less code to be compiled.

    Look at the compiled assemblies using Reflector.  You will see what I mean.

    Hope this helps,

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    Thursday, April 05, 2012 10:24 PM
  • I really do not understand why the compiler is sensitive to some headers and not to others. Why the Compiler is sensitive to <iostream> and not to <windows.h> ? why the Compiler is sensitive to <string> and not to <stdio.h> ? Taking into account that I didn't use any code of these headers that mean I just used the #include statement. It seems that it is not recommended for some reason to include a Console header like <iostream> or <string> inside a "Windows" application. In the same way do not include a windows header like <Windows.ApplicationModel.h> inside a Console application.





    Monday, April 09, 2012 6:43 PM
  • It is not the C++ Compiler that is causing the difference.  It is the source code itself encountering conditional compilation statements.  Believe it or not, the entire file is parsed by pre-processor when it is compiled for both projects.  That does not mean that the entire file's content is compiled.  In fact, in neither case is all of the code in the files compiled into the output assembly.

    The #if, #elif, #else, and #endif Directives (C/C++)   

    http://msdn.microsoft.com/en-us/library/503x3e3s(v=VS.80).aspx  C++ macros

    Preprocessor Directives (C/C++) - MSDN – Explore Windows, Web ...   

    Again, what you are seeing is "by design".  Specifically, by software design of the source code. 

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    • Proposed as answer by Rudedog2MVP Tuesday, April 10, 2012 9:32 PM
    • Unproposed as answer by Ahmed Shakir Wednesday, April 11, 2012 11:25 AM
    Monday, April 09, 2012 9:29 PM
  • So, In your opinion,  how to overcome such a problem? I mean if I need to use something from <iostream> or <string> or <complex> in my "Windows Application Form" how can I use it without effecting the size of my exe file?
    Tuesday, April 10, 2012 7:00 PM
  • So, In your opinion,  how to overcome such a problem?

    Accept it.  It is not a problem.  It is a fact of life, that is how the code is written.  It compiles differently for different project types.  It probably compiles differently for other conditions, too, conditions like OS, or even 32 bit or 64 bit CPUs.  Like I said before, take a look at the compiled assemblies using a tool like Reflector.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Tuesday, April 10, 2012 9:32 PM
  • If I accept this behavior(i.e large file size), then when I port my program to another OS it will not work! I am using Windows7 and I port my program to Vista. Now I just removed the line  #include<iostream> and rebuild my program and then port it to Vista, It worked fine. How can you explain this?
    Wednesday, April 11, 2012 11:24 AM
  • If I accept this behavior(i.e large file size), then when I port my program to another OS it will not work! I am using Windows7 and I port my program to Vista. Now I just removed the line  #include<iostream> and rebuild my program and then port it to Vista, It worked fine. How can you explain this?

    I lack enough information to explain it.  You appear to fail to grasp the fact that the size of the EXE has no relevance to the compiler.  None.

    You made some sort of mistake, or introduced some type of coding error, when you tried to "port" your program, which caused it to "not work."

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Thursday, April 12, 2012 9:41 PM
  • If you follow the steps above, you find out that I did not write any code so as to got the chance to make some sort of mistake or introduce some type of coding error! I just start a new project as "Windows Application Form" and everything is written for me by the Visual Studio wizard. My added code is two words: #include<iostream> and nothing more. I copy paste here what I mean:

    #pragma once
    #include<iostream>
    namespace WindowsFormsApplication1 {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary>
    	/// Summary for Form1
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->components = gcnew System::ComponentModel::Container();
    			this->Size = System::Drawing::Size(300,300);
    			this->Text = L"Form1";
    			this->Padding = System::Windows::Forms::Padding(0);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    		}
    #pragma endregion
    	};
    }
    

    Friday, April 13, 2012 2:06 AM
  • OK, I found the cause for the large size of exe file but please don't ask why because I don't have an answer:

    THE SOLUTION:

    1. Go to your project property. 
    2. Select Configuration Properties -> General
    3. Under the Project Defaults find "Common Language Runtime Support" and change it from "/clr:pure" to "/clr"
    4. That's all. Rebuild your project and you find your exe file go back to a small size file again! port it to other OS like Vista without a problem.
    • Marked as answer by Ahmed Shakir Friday, April 13, 2012 4:18 AM
    Friday, April 13, 2012 4:18 AM
  • You still don't understand conditional compilation.  Changing that constant changes how the source code is parsed.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Friday, April 13, 2012 11:10 AM
  • Thanks you all in trying to help me. I wish you the best. My application now is small exe size, and can be run under all Windows versions from Windows2000 to Windows7. Its free to download from my web site: http://www.ges.net63.net  Please try it and give me your opinions.

    Regards

    Ahmed

    structural_ahmed@yahoo.com

    Friday, April 13, 2012 4:01 PM