The usual reason is because you don't have access to the code of main().
You're writing a library and you want to be sure that certain
Has nothing to do with main but with the method the library gets
loaded. So the right point would be to check the OS how it works while
it loads a library. The OS may (or may not) have an interface that
gets called when the library gets loaded. Has nothing to do with C but
C knows nothing about libraries too.
A more exotic reason is to have some C code automatically executed before
a program written in another language starts its own execution.
It is main() that gets automagically executed when a program gets
executed. Between the OS an main there is the C Runtime Library that
does the magic: initialison of static and extern class variables,
creating heap, stack or equivalent things. Ask your compiler if it is
open enough to describe the internals. Ask the other language
compilers how it builds its interfaces to C.
The simplest method would be to build a library thet can called from
other languages, using the loader interface to make own initialisons.
If all that is really impossible then use a trick to preserve the work
with uninitialised data. Use a static variable, initialise it
statically with an value that idetifies the library as uninitialised -
and thest in each API that variable for its state and either let the
app fail or make the initialisons under cover then.
main() is the name of the entrypoint a C program gets called from the
RTL. The OS calls always the RTL when it activates the execution of an
application. So if your main is written in another language then the
RTL of that language is executed before the main of that app. gets
executed. Even the C RTL will then not initialised.
Each library has its own entry point that gets called from the system
loader when the library gets loaded.
But all that is higly implementation defined, because there is
absolutely nothing in the standard that describes how an
implementation has to hande the startup of an application and/or a
library. So you've study the relevant interfaces of your OS and your
compiler to find the answer.
--
Tschau/Bye
Herbert Rosenau
http://www.pc-rosenau.de eComStation Reseller in Germany
eCS 1.1 GA englisch wird jetzt ausgeliefert