Next: , Previous: Features, Up: Features


6.1 Startup codes

There is a lot of work to do before your ‘main’ function can be called - open shared libraries, open stdin, stdout, etc. Depending on the compiler options and the ANSI functions you use. This work is done by the startup code.

To get a short startup all the necessary modules are optional - they get only linked in if you use them. There are 2 exceptions from this (since the linker cannot check for it):

The startup codes itself are written in assembly to be as short as possible.

Here is a little program to get the point:

     #include <inline/exec.h>
     #include <dos/dos.h>
     #include <inline/dos.h>
     #include <workbench/workbench.h>
     
     int __nocommandline=1; /* Disable commandline parsing  */
     int __initlibraries=0; /* Disable auto-library-opening */
     
     struct DosLibrary *DOSBase=NULL;
     
     extern struct WBStartup *_WBenchMsg;
     
     int main(void)
     { if(_WBenchMsg==NULL)
       { if((DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37))!=NULL)
         { Write(Output(),"Hello world\n",12);
           CloseLibrary((struct Library *)DOSBase); } }
       return 0;
     }

compiled and linked with

gcc -noixemul -s -O2 -fbaserel helloworld.c

gives an executable of 492 bytes. And this with the normal ‘main’ function!

So you never need to try to write a program without a startup code.