General
cancel
Showing results for 
Search instead for 
Did you mean: 

atexit() does it have any twin brother?

Wkdunreal
Advisor

atexit() does it have any twin brother?

Hi pros,

I have been using atexit() for some time and now there is a need for me for a function that coudl call something for me just before start of
int main() or void main()

Like atexit() does there exist something like atstart()?

What I intend to do with this so called "atstart()" is to call a function which sets some flag.

Now I have access only to libraries and can modify libraries only, I cannot access the file which contains

void main()..

and hence the reason why i need something like this.

Could some1 tell me if something like this exists? :) Could not find anything :(


One more thing.

When main() starts, is there some internal flag or parameter which is set? So that I can access it and use it in the libraries?.

Thanks a Lot
Regards :)
10 REPLIES
Horia Chirculescu
Honored Contributor

Re: atexit() does it have any twin brother?

Hello,

I fail to understand the purpose of this function "atstart()".

Why not calling your function inside the main() function, at the very begin of this program?

You can allways pass some arguments to the main() function and that should serve right if you need to do some checking/whatever.

Horia.
Best regards from Romania,
Horia.
Steven Schweda
Honored Contributor

Re: atexit() does it have any twin brother?

VMS has LIB$INITIALIZE, but I know of nothing
similar elsewhere. I've never tried it, but
I'd guess that you'd need to write your own
"mainx()" function, which would do your extra
work, and which would then call "main()"
(with the appropriate arguments). And that
you'd need to pass an appropriate "-e epsym"
option to "ld" when you linked the
executable. Or something like that.

man ld
Wkdunreal
Advisor

Re: atexit() does it have any twin brother?

Hi Horia,

Thanks for reply :)

1)
The reason i need the "atstart()" is because I do not have access to the file containing
the main() function.Am working on an existing product.

I am developing some wrapper library for existing functions and am not allowed to touch the main() file.

2)Since have no access to the main() file, I cannot pass any parameters to main() and get the required value.

Thanks and Regards
Steven Schweda
Honored Contributor

Re: atexit() does it have any twin brother?

> [...] am not allowed to touch the main()
> file.

What is "the main() file"? The existing
executable? An object file? If you are not
linking the thing yourself, then you may have
some real trouble.

> [...] I cannot pass any parameters to
> main() and get the required value.

Not even from the command line? If _you_
call "main()", then don't _you_ specify the
parameters?

Perhaps you should begin again, and explain
your situation more clearly.

> Like atexit() does there exist something
> like atstart()?

Who would call your "atstart()"? The linker
is the thing which sets the entry point for
an executable. (Under the direction of the
C compiler (driver), if that's what you use
to link the executable. The linker is not
born knowing that you want it to be
"main()".)
Dennis Handly
Acclaimed Contributor

Re: atexit() does it have any twin brother?

What OS version and architecture are you using?

>atexit() does it have any twin brother?

(You are using the wrong adjective, twin means same.)
C++ has static construction and destruction, done before main and after exit, respectively.

>...void main()

(This is illegal. Only int main is valid.)

>When main() starts, is there some internal flag or parameter which is set?

Not that I know of. Why do you care? If you are running a shlib initializer, you already know how to call something before main.

>Horia: I fail to understand the purpose of this function "atstart()".

This is also called a init function.

>Steven: that you'd need to pass an appropriate "-e epsym" option

Leave well enough alone, -e not for use by ordinary users, especially on Integrity.

>Who would call your "atstart()"?

dld of course. -Wl,+init,function_name

>The linker is the thing which sets the entry point for an executable.

You don't want to change the entry point, which must be in libc.

>The linker is not born knowing that you want it to be main.

But libc knows that.
Horia Chirculescu
Honored Contributor

Re: atexit() does it have any twin brother?

>"I am developing some wrapper library for existing functions ..."

Are you trying to use some functions that you can acces from some executable file (the one that have the main() function that you are talking about), and those functions must be called by you in some controlled environment that you must set or what?

Best regards,
Horia.
Best regards from Romania,
Horia.
Steven Schweda
Honored Contributor

Re: atexit() does it have any twin brother?

> (You are using the wrong adjective, twin
> means same.)

You are using the wrong definition of "twin",
which doesn't mean "same".

http://en.wikipedia.org/wiki/Twin

> [...] -Wl,+init,function_name

That looks potentially useful, however.
Bob E Campbell
Honored Contributor

Re: atexit() does it have any twin brother?

As a father of twin daughters born on my birthday I have to agree that twins can be radically different.

That said, I trust Dennis in this space. If you feel that you are stuck you might want to give a very specific problem to solve. There is always another way...
Dennis Handly
Acclaimed Contributor

Re: atexit() does it have any twin brother?

Do you have a specific time to call this function? Basically initializers can be done in this order:
rightmost-shlib, next ... executable ... main

>Steven: You are using the wrong definition of "twin", which doesn't mean "same".
http://en.wikipedia.org/wiki/Twin

It doesn't help you aren't using a dictionary or one that doesn't discuss other that meaning #1. :-)
Better definitions are:
http://en.wiktionary.org/wiki/twin
(#2)
http://www.yourdictionary.com/twin
1b: being one of a pair of such things; being a counterpart
(Saying evil twin may be better. :-)
Steven Schweda
Honored Contributor

Re: atexit() does it have any twin brother?

> It doesn't help [...]

I didn't think that you'd need more help than
that. And, apparently, I was right.

> [...] twins can be radically different.

Yup. Matching DNA does not mean "identical".
(I have a friend with a pair of daughters
which appear very similar -- to a casual
observer. And their divergence seems to grow
as they do.)