entry at other than main (was want to know)

Engineering scott at bbxeng.UUCP
Tue Aug 22 08:51:58 AEST 1989


In article <15127 at dartvax.Dartmouth.EDU> ari at eleazar.dartmouth.edu (Ari Halberstadt) writes:
>In article <19164 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>>In many articles many people write this, that, and the other argument
>>for or against `main()' as the program entry point.
>[lots of very good arguments]
>
>Ok, there's actually a very simple way to do this. Just have an option
>to the linker telling it what the entry point is. This is done on the
>Macintosh with MPW.

[flame on]

OK.  For the *last time*, folks:

There is nothing *special* about 'main'.  The UNIX/XENIX linker doesn't
give a $&#$# about 'main'.  Before a 'C' program can execute, a startup
routine must be executed to set up a few things (such as the arguments
to main()).  This startup routine is usually found in the file 'crt0.o'.
When the linker is called from the 'C' compiler, the file 'crt0.o' is quietly
given as the first module in the link.  The linker simply considers the
first instruction in the first module to be the entry point.  Some 
linkers will vary but the idea is the same - there is an known entry
point in 'crt0.o'.  (Perhaps the symbol table for 'crt0.o' contains
an explicit entry point).

If you invoke 'ld' yourself you must remember to include 'crt0.o' or
you will probably get error messages.

Once 'crt0.o' does its thing *it* calls main().  If you want *it* to call
some other routine then either modify the source for 'crt0.o' or
write your own startup routine.

Please understand, 'main' is *not* the entry point to your program
as far as UNIX is concerned.  It may *appear* to be the entry point
in *your* code because of a (usually invisible) startup routine that
is designed that way.

I, for one, have not lost any sleep over this.

[flame off]

-- 

---------------------------------------
Scott Amspoker
Basis International, Albuquerque, NM
505-345-5232



More information about the Comp.lang.c mailing list