Thread: #include and file size

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    #include and file size

    I thought this was a kind of interesting experiment (results included in comments in the file):

    Code:
    /*
    	Testing how unnecessary #includes influence
    	the size of the compiled file
    
    	Compiled in VC++ 2008, here are the file sizes for the executable:
    	1) completely empty program with no #include: 27KB for .exe; 294KB for linker
    	2) #include <iostream>: 27KB; 302KB for linker file
    	3) #include <iostream> and #include <string>: 27KB; 302KB for linker 
    	4) using namespace std; : 27KB; 314KB for linker
    	5) with both #includes as well as minimal code (no using): 33KB for .exe; 319KB for linker
    
    	Conclusion here: #include has no influence on the size of the .exe but only
    	on the linker file--and even there the influence is pretty small
    	Only the USE of the libraries increases size of executable.
    
    	Programmer: Marshall Farrier
    	Date: 12/21/09
    */
    
    #include <iostream>
    #include <string>
    //using namespace std; // for 4)
    
    int main()
    {
    	std::string str = "Hasta la vista!\n"; // added for 5)
    	std::cout << str; // added for 5)
    	return 0;
    }
    Dumb question: In order to run this outside of an IDE, exactly which file(s) would be needed? Just the .exe? or is the incremental linker file also necessary?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know what they're referring to here by the linker.
    You only need the exeutable and the vs runtime. If you link statically, you only need the exe.
    This is since you don't use any other dependencies.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    I think that is quite clear - if something isn't used, compiler doesn't need to generate code to call it.

    What do you mean with linker file?

    EDIT: overspeed

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by fronty View Post
    I think that is quite clear - if something isn't used, compiler doesn't need to generate code to call it.

    What do you mean with linker file?

    EDIT: overspeed
    Or, in the case of dynamic linking which is probably more commonly used these days, doesn't need to load the library at boot.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Here's a header that will increase your EXE size:

    Code:
    // bloat.h
    
    namespace { char bloat[1024*1024]; }
    Headers TYPICALLY don't include anything that would increase code size, but it doesn't mean they CAN'T.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You need to initialize that, or it won't actually cause the EXE to grow.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    242
    "linker" is just the term I'm using to refer to a file listed as "Incremental Linker File" and showing up in the Debug folder along with the .exe. It has the suffix .ilk

    Sounds like it's not important for independent execution, though.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not important for the executable.
    It's simply a file that the linker generates so it can assemble an exe more quickly (ie incrementally building the file instead of re-doing it from scratch).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Header file include order
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2006, 03:22 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM