home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19147 < prev    next >
Encoding:
Text File  |  1993-01-03  |  2.8 KB  |  87 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!usenet-feed.cc.umr.edu!mcastle
  3. From: mcastle@cs.umr.edu (Michael R Castle)
  4. Subject: Re: [FAQ] size of executables ?
  5. References: <93003.004225RVK@psuvm.psu.edu>
  6. Date: Sun, 3 Jan 1993 07:14:30 GMT
  7. Nntp-Posting-Host: next9.cs.umr.edu
  8. Organization: University of Missouri - Rolla, Rolla, MO
  9. Sender: cnews@umr.edu (UMR Usenet News Post)
  10. Message-ID: <1993Jan3.071430.11936@umr.edu>
  11. Lines: 74
  12.  
  13. In article <93003.004225RVK@psuvm.psu.edu> <RVK@psuvm.psu.edu> writes:
  14. >Hi, my question may be a faq, but I am asking it nevertheless.
  15. >
  16. >How does one compile a c program so that the size of the
  17. >executable is as small as possible. I noticed that even a
  18. >trivial program like Hello.c  produces a huge executable,
  19. >perhaps due to the include files stdio.h etc. Of course,
  20. >I can not do any i/o without stdio.h . On the other hand,
  21. >I am not using all the stuff that's there in stdio.h, and
  22. >feel that the size of the executable should be proportional
  23. >to what I am using. Any suggestions ?
  24.  
  25. printf() is huge.  Anyprogram that uses printf() has the potential for creating
  26. a large exectuable.
  27.  
  28. Try this:
  29.  
  30. #include <stdio.h>
  31.  
  32. int main(void) {
  33.   printf("line 1\n");
  34. }
  35.  
  36.  
  37. then:
  38.  
  39. #include <stdio.h>
  40.  
  41. int main(void) {
  42.   printf("line 1\n");
  43.   printf("line 2\n");
  44. }
  45.  
  46.  
  47. Compare the size of the executables.  Probably pretty close.  It's not so
  48. much how many function calls, but what functions you are calling.
  49.  
  50. I imagine puts() is much smaller than printf().
  51.  
  52. Try
  53.  
  54. #include <stdio.h>
  55.  
  56. int main(void) {
  57.   puts("line 1\n");
  58. }
  59.  
  60. And see if that is any smaller than using printf().
  61.  
  62. Of course, then you loose functionallity.
  63.  
  64. You could try to simulate printf() functionallity using sscanf() and puts()
  65. but I imagine sscanf is nearly as large as printf() so you wouldn't get 
  66. anyway. (Maybe both link in the same code?)
  67.  
  68. Anyway, smart linkers (like Borlands) does try to link in only function that
  69. are actually called, so not all the functions listed in stdio.h are linked in.
  70.  
  71. Matter of fact, man compiler (at least on unix systems) are putting more and
  72. more of all the prototypes into one (large) .h file.  I imagine the original
  73. reason for not including all the of the standard system libs into one large
  74. file in the first place was because of slower systems.  Filtering out all the
  75. stuff in the .h file you don't actually use was too slow.  Now days, compilers
  76. are usually fast enough that that extra overhead is not noticable (of course,
  77. this limits defining your own functions that have the same name, but oh
  78. well).
  79.  
  80. enough ramblings,
  81. mrc
  82. -- 
  83. Mike Castle .-=NEXUS=-.  Life is like a clock:  You can work constantly
  84.   mcastle@cs.umr.edu     and be right all the time, or not work at all
  85. S087891@UMRVMA.UMR.EDU   and be right at least twice a day.  -- mrc
  86.     We are all of us living in the shadow of Manhattan.  -- Watchmen
  87.