www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/15/10:39:12

Message-ID: <D1FB30BBA491D1118E6D006097BCAE39261C50@Probe-nt-2a.Probe.co.uk>
From: Shawn Hargreaves <ShawnH AT Probe DOT co DOT uk>
To: djgpp AT delorie DOT com
Subject: Re: startup functions on gnu c/c++?
Date: Tue, 14 Apr 1998 10:41:40 +0100
MIME-Version: 1.0

> 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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019