NO

Author Topic: How do I make a smaller output file?  (Read 5041 times)

Ben321

  • Guest
How do I make a smaller output file?
« on: February 04, 2016, 09:17:46 AM »
My files are huge, for even a very simple program. I'm getting about 19kb for an EXE or DLL file that only does something simple like printf("%d",1+1"); or return 1+1;.  Seriously there's no reason that it should be so huge. In ASM code, writing an EXE or DLL file that did these things would get me something like a 1kb to 2kb file, not a nearly 20kb file. Why is your compiler making such a big file for such a simple task? Is there any way I could optimize it so that it will generate only the bare minimum sized file needed to accomplish the given task? Basically, can't your program convert C code into the equivalent ASM code, and then assemble that into a DLL or EXE file?

Ben321

  • Guest
Re: How do I make a smaller output file?
« Reply #1 on: February 04, 2016, 09:21:12 AM »
Can somebody move this thread to this http://forum.pellesc.de/index.php?board=2.0 forum section? I didn't see the other section until I scrolled farther down that page, but other forum section I see is specifically about Pelles C, which is what my question is specifically about, not C programming in general.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: How do I make a smaller output file?
« Reply #2 on: February 04, 2016, 02:20:51 PM »
The exe is large because it takes the full static C runtime library. The C runtime is required to init the basic structures of C standard libraries (File, I/O, math, ecc) and contain the standard C functions.
This is true for all compilers including a static runtime (i.e. MS is even larger because the runtime includes C++ stuff).
To reduce executable dimensions you have 2 options: the firat is to comppile with DLL runtime library. You can select the option in project->options, compiler tab, runtime library. Select Multithreaded DLL.
To squeeze runtime size even more the second option is to link with a tiny runtime like tinyClib for Consolle programs and a more complex http://wcrt.sourceforge.net/ for GUI apps.
You can even write a piece of code in C and direct execution at your routine, using the switch /ENTRY=<Start_Symbol>. Use the switch /NODEFAULTLIB to avoid the linkage of runtime.

Please as standard usage you are supposed to search the forum for similar questions before to ask again, in fact if you search you'll find a lot of comments on the issue.
« Last Edit: February 04, 2016, 02:22:31 PM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: How do I make a smaller output file?
« Reply #3 on: February 05, 2016, 08:33:01 AM »
With msvcrt.lib/msvcrt.dll it is possible to make small executable too.
Code: [Select]
int printf(const char * restrict format, ...);
void exit(int status);
#pragma comment(lib, "msvcrt.lib")

void __cdecl mainCRTStartup(void)
{
printf("Hello World\n");
exit(0);
}
Code: [Select]
#pragma comment (lib, "msvcrt.lib")
//#pragma comment(linker,"/merge:.rdata=.data")
#pragma comment(linker,"/subsystem:console,5.1") // for ms link 10

int __cdecl printf(const char *format, ...);
void __cdecl mainCRTStartup(void)
{
int __cdecl main(int argc, char **argv);
int __cdecl __getmainargs(int*, char***, char***, int, void*);
void __cdecl exit(int status);

int    argc;
char** argv;
char** env;
int    si = 0; // XP needs this

__getmainargs(&argc,&argv,&env,0,&si);
exit(main(argc,argv));
}
int main(int argc, char **argv)
{
printf("Hello C\n");
return 0;
}
« Last Edit: February 10, 2016, 02:25:42 PM by TimoVJL »
May the source be with you

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: How do I make a smaller output file?
« Reply #4 on: February 07, 2016, 07:14:47 AM »
My files are huge, for even a very simple program. I'm getting about 19kb for an EXE or DLL file that only does something simple like printf("%d",1+1"); or return 1+1;.  Seriously there's no reason that it should be so huge. In ASM code, writing an EXE or DLL file that did these things would get me something like a 1kb to 2kb file, not a nearly 20kb file. Why is your compiler making such a big file for such a simple task? Is there any way I could optimize it so that it will generate only the bare minimum sized file needed to accomplish the given task? Basically, can't your program convert C code into the equivalent ASM code, and then assemble that into a DLL or EXE file?
As others already stated, you are compiling a C program, which, without special precautions, will always include a bit more of general run time library code than a plain assembler file.
For practical Windows programming (beyond such trivial examples as you mentioned), you will very quickly reach the point where the added size of the C libraries compared to the same tasks in assembler will be far more efficient when you compare the two compared by the time it takes for you to develop, debug and maintain any non-trivial program...

Ralf