www.digitalmars.com         C & C++   DMDScript  

c++ - Calling func before main()

reply "Karol Gottan" <gottan op.pl> writes:
Hi,

I am looking for a way how to call a desired
function before main() is called in C mode ?

Something like
    #pragma startup foo
in other compilers.

--

Karol Gottan
Feb 01 2004
parent reply Jan Knepper <jan smartsoft.us> writes:
Very simple... ;-)



static int     result_of_foo = foo ();



int  main ( int, char **, char ** )
{
    return (  0 );
}



static int  foo ()
{
    // Do something before 'main' is being invoked.

    return (  0 );
}


HTH


Karol Gottan wrote:
 Hi,
 
 I am looking for a way how to call a desired
 function before main() is called in C mode ?
 
 Something like
     #pragma startup foo
 in other compilers.
 
 --
 
 Karol Gottan
 
 
-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Feb 01 2004
next sibling parent reply "Karol Gottan" <gottan op.pl> writes:
<jan smartsoft.us> wrote :

 Very simple... ;-)
Unfortunately I am getting this : ------------ static int result_of_foo = foo (); ^ test.c(3) : Error: constant initializer expected ------------ Thanks for jumping in. -- Karol
Feb 01 2004
parent reply Scott Michel <scottm cs.ucla.edu> writes:
Karol Gottan wrote:

 <jan smartsoft.us> wrote :
 
 Very simple... ;-)
Unfortunately I am getting this : ------------ static int result_of_foo = foo (); ^ test.c(3) : Error: constant initializer expected ------------
You're not compiling C++ code, are you? -scooter
Feb 02 2004
parent "Karol Gottan" <gottan op.pl> writes:
<scottm cs.ucla.edu> wrote :


[...]

 You're not compiling C++ code, are you?
In my first attempt yes - I did not compile in C++ mode. But then I realised I did it wrong and added -cpp. -- Karol
Feb 02 2004
prev sibling parent reply Jan Knepper <jan smartsoft.us> writes:
Jan Knepper wrote:

 Very simple... ;-)
 
 
static int foo ();
 
 static int     result_of_foo = foo ();
 
 
 
 int  main ( int, char **, char ** )
 {
    return (  0 );
 }
 
 
 
 static int  foo ()
 {
    // Do something before 'main' is being invoked.
 
    return (  0 );
 }
 
 
 HTH
 
 
 Karol Gottan wrote:
 
 Hi,

 I am looking for a way how to call a desired
 function before main() is called in C mode ?

 Something like
     #pragma startup foo
 in other compilers.

 -- 

 Karol Gottan
-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Feb 01 2004
next sibling parent "Karol Gottan" <gottan op.pl> writes:
<jan smartsoft.us> wrote :
 Jan Knepper wrote:

 Very simple... ;-)
static int foo ();
Thanks ! Works perfectly ! -- Karol
Feb 01 2004
prev sibling parent reply "Steve Strand" <snstrand comcast.net> writes:
Another way is to define a class at global level that has a constructor.
The constructor will be called before main().
Example:

struct foo {
    foo() {cout << "do stuff before main\n";}
    ~foo() {cout << "do stuff after main\n";}
} myfoo;

int main()
{
    cout << "here we are in main\n";
}
Feb 01 2004
parent reply Scott Michel <scottm mordred.cs.ucla.edu> writes:
Before someone takes the initiative and starts elaborating on this example,
this question is a comp.lang.c++ FAQ item, whihc also covers how to sequence
object allocation and construction before main() is called.


Steve Strand <snstrand comcast.net> wrote:
 Another way is to define a class at global level that has a constructor.
 The constructor will be called before main().
 Example:
 
 struct foo {
    foo() {cout << "do stuff before main\n";}
    ~foo() {cout << "do stuff after main\n";}
 } myfoo;
 
 int main()
 {
    cout << "here we are in main\n";
 }
Feb 01 2004
parent Jan Knepper <jan smartsoft.us> writes:
<g> I was thinking that the question would come from a C++ classes book 
or something like that.

It is one of the standard questions employers will ask you during a 
technical job interview for a C/C++ coding job...



Scott Michel wrote:
 Before someone takes the initiative and starts elaborating on this example,
 this question is a comp.lang.c++ FAQ item, whihc also covers how to sequence
 object allocation and construction before main() is called.
 
 
 Steve Strand <snstrand comcast.net> wrote:
 
Another way is to define a class at global level that has a constructor.
The constructor will be called before main().
Example:

struct foo {
   foo() {cout << "do stuff before main\n";}
   ~foo() {cout << "do stuff after main\n";}
} myfoo;

int main()
{
   cout << "here we are in main\n";
}
-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Feb 01 2004