Search code examples
c++cstartupprogram-entry-point

Call a function before main


Possible Duplicate:
Is main() really start of a C++ program?

Is possible to call my function before program's startup? How can i do this work in C++ or C?


Solution

  • You can have a global variable or a static class member.

    1) static class member

    //BeforeMain.h
    class BeforeMain
    {
        static bool foo;
    };
    
    //BeforeMain.cpp
    #include "BeforeMain.h"
    bool BeforeMain::foo = foo();
    

    2) global variable

    bool b = foo();
    int main()
    {
    }
    

    Note this link - Mirror of http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14 / proposed alternative - posted by Lundin.