Invoking a function before main

Invoking a function before main

Post by manueljo » Tue, 22 Jul 2003 08:55:00


How Can I invoke a function before the execution of main() in C.

Regards
Manuel
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Philipp Th » Wed, 23 Jul 2003 17:08:32


There's no standardized and portable way of doing so. gcc offers
the attribute constructor for doing this.

Philipp
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM

 
 
 

Invoking a function before main

Post by Sven Semml » Wed, 23 Jul 2003 17:08:57


There is no way to do that. If you are programming a library you might
want to implement main() and call some user_main() callback inside. Then
you require the library user to implement user_main() instead of main().

Just like some guys did with their GUI library ... WinMain(), you know ;)

Sven

--
Sven Semmler http://www.yqcomputer.com/
GPG Fingerprint: 72CA E26D C2A3 1FEB 7AFC 10EA F769 A9A4 937F 5E67
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Hans-Bernh » Wed, 23 Jul 2003 17:09:11


In general you can't, because there's no portable method of doing that.

This is entirely platform-specific, so please again in a forum
dedicated to whatever your platform happens to be.
--
Hans-Bernhard Broeker ( XXXX@XXXXX.COM )
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Francis Gl » Wed, 23 Jul 2003 17:09:43

In article < XXXX@XXXXX.COM >, Manuel
< XXXX@XXXXX.COM > writes

You can't, main() IS the start of your program.

--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Erik Truls » Wed, 23 Jul 2003 17:10:16


You can't.

There might be some implementation-specific way of doing this. To find
out if there is and what it is you need to check the documentation of
your compiler. Any such method is definitely not portable in any way.

--
<Insert your favourite quote here.>
Erik Trulsson
XXXX@XXXXX.COM
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Mark Mynst » Thu, 24 Jul 2003 02:59:19

>>>>> "Manuel" == Manuel < XXXX@XXXXX.COM > writes:
Manuel> How Can I invoke a function before the execution of main() in C.

I know this does not answer your question, but why would you want to
do that?

--
-MM
I rarely read email from this address /"\
because of spam. \ / ASCII Ribbon Campaign
I MAY see it if you put #NOTSPAM# X Against HTML Mail
in the subject line. / \
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Rob » Thu, 24 Jul 2003 02:59:22


In general you can't, and will need to resort to using compiler
specific techniques.

In C++ it is possible to do this; the constructors of static
objects declared outside any function will be invoked before
main().
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Oleg Golds » Thu, 24 Jul 2003 02:59:37


XXXX@XXXXX.COM (Manuel) writes:


I don't think you can do that in C. You can in C++, but that's a
different language...

--
Oleg Goldshmidt | XXXX@XXXXX.COM
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Mark Gordo » Sat, 26 Jul 2003 01:27:48

On 22 Jul 2003 17:59:19 GMT



One valid reason is to program your MMU on an embedded system so that
you can actually access memory when main starts. Of course, on the
compilers targeting embedded systems they provide you with non-standard
ways to do this.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by airi » Sat, 26 Jul 2003 01:27:53


I recall an old uni project where we had to supply a emulator for a
robot car. Users of the real robot car wrote a C program which was
cross compiled and uploaded to the car before it was put through it's
trials. The user program used the car's sensors to attempt to navigate
an unknown terrain. [Like the Mars rovers, only much cheaper, or Robot
Wars, only less destructive! :-)]

The library was designed to replace the robot's sensor API with a
simulation. Thus enabling users (school kids) to test their programs
on a simulator.

The library obviously didn't have access to main() as that was a user
program and it would be tedious to modify each new program each time.
Nontheless library initialisation needed to be performed before it
could work. The trouble was, there was no guarantee of which API
function would be called first.

The solution was trivial: each API emulation function tested a static
boolean and called an initialisation function the first time one of
the functions were called.

static int lib_initialised;

void one_of_many_functions(/* ...yadda... */)
{
if (!lib_initialised) initialise_lib();

/* ... */
}

Not very pretty, but it worked.

Peter
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Mark Mynst » Sun, 27 Jul 2003 22:22:02

>>>>> "Peter" == Peter Nilsson < XXXX@XXXXX.COM > writes:


Peter> The library obviously didn't have access to main() as that was a user
Peter> program and it would be tedious to modify each new program each time.
Peter> Nontheless library initialisation needed to be performed before it
Peter> could work. The trouble was, there was no guarantee of which API
Peter> function would be called first.

Peter> The solution was trivial: each API emulation function tested a static
Peter> boolean and called an initialisation function the first time one of
Peter> the functions were called.

Peter> static int lib_initialised;

Peter> void one_of_many_functions(/* ...yadda... */)
Peter> {
Peter> if (!lib_initialised) initialise_lib();

Peter> /* ... */
Peter> }

I am not sure I know what you are saying... I guess that is something
like when _init is not exportable? I suspect that _init was not
possible at that time.


--
-MM
I rarely read email from this address /"\
because of spam. \ / ASCII Ribbon Campaign
I MAY see it if you put #NOTSPAM# X Against HTML Mail
in the subject line. / \
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by Mark Mynst » Sun, 27 Jul 2003 22:22:04

>>>>> "MG" == Mark Gordon < XXXX@XXXXX.COM > writes:

MG> One valid reason is to program your MMU on an embedded system so that
MG> you can actually access memory when main starts. Of course, on the
MG> compilers targeting embedded systems they provide you with non-standard
MG> ways to do this.

It seems to me that any mechanism to invoke a function prior to main
would be non-standard. (Access to memory would be a good thing, though.)

:-)

--
-MM
I rarely read email from this address /"\
because of spam. \ / ASCII Ribbon Campaign
I MAY see it if you put #NOTSPAM# X Against HTML Mail
in the subject line. / \
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM
 
 
 

Invoking a function before main

Post by airi » Tue, 29 Jul 2003 10:56:23


...
...

No more than <stdio.h> is a fundamentally flawed framework. There is
no initialisation proceedure; you can call fopen() whenever you like.

The real library was simply a set of stubs for rom routines which
otherwise relied on the robot's bootstrap to do all the relevant
hardware initialisation. Hence, there was no need for any user called
initialisation function.

[resequenced]

As I recall, that either wasn't an option, or was frowned upon.

Yes, it's certainly possible to 'move the user's main', but that would
have required development tool specific solutions at a deeper level
than merely linking in an alternative library.

Peter
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM