Mail Archives: djgpp/1998/04/15/10:39:12
> How to I define functions to be called on program startup before main
> is called?
The only portable way to do this is to write in C++ and make your
function be the constructor for a global object.
There is a non-standard gcc extension, the 'constructor' function
attribute, which can be used to do this with a normal C routine, but
it probably isn't a good idea to rely on this unless you are 100%
certain that your code will never need to be ported to any other
compilers. In most cases where you might wish to use such a routine,
I think it would actually be better to make an explicit call to the
setup code from the start of main(), or store a 'firsttime' flag
in your module which it can use to automatically initialise itself
the first time it gets called.
See 'info gcc "c extensions" "function attributes"' for details about
how to set up a constructor function.
Shawn Hargreaves.
- Raw text -