delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/30/00:15:05

Date: Wed, 30 Jul 1997 16:16:36 +1100
From: Bill Currie <billc AT blackmagic DOT tait DOT co DOT nz>
Subject: Re: custom entry/exit code
In-reply-to: <33DC0947.605F@xl.ca>
To: djgpp AT delorie DOT com
Message-id: <199707300421.QAA07681@teleng1.tait.co.nz gatekeeper.tait.co.nz>
Organization: Tait Electronics Limited
MIME-version: 1.0
Comments: Authenticated sender is <billc AT blackmagic DOT tait DOT co DOT nz>

On 27 Jul 97 at 19:51, Darren Grant wrote:

> Hello,
> 
> How do I link my own extra entry/exit code into a c project?
> 

The easiest way is to use gcc's constructor and destructor 
attributes. ie:

static void __attribute__((constructor)) entry_function(void)
{
	/*do init stuff */
}

static void __attribute__((destructor)) exit_function(void)
{
	/* do uninit stuff */
}

These functions will be called before main and after exit, 
respectively (exit is called with main's return value).

If you have several object files with constructor/destructor 
functions, the order they are called in is determined by the link 
order.  I don't know whether destructors are called in forward or 
reverse order, but the constructors are called in same order that the 
object files are specified on the linker command line.

Bill
--
Leave others their otherness.

- Raw text -


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