Search code examples
c++windowsmicrosoft-runtime-library

How to explicitly call CRT startup functions


Is there any way I can explicitly in my code call the functions that are normally called in mainCRTStartup functions? I'm specifically interested initterm and initterm_e functions. Even thought my program is linked with crt (it has to be, right?) I couldn't just call initterm - I tried to declare and just invoke and I got undefined reference.

Is there any way to do it?


Solution

  • the _initterm, and _initterm_e (note in msdn _initterm_e is wrong declared - really it used _PIFV type) is exported by different CRT dlls. and it declared in lib files too. and implementation of this functions is very simply:

    typedef void (__cdecl *_PVFV)();
    
    void _initterm(const _PVFV *ppfn, const _PVFV *end)
    {
        do 
        {
            if (_PVFV pfn = *++ppfn)
            {
                pfn();
            }
        } while (ppfn < end);
    }
    
    typedef int  (__cdecl *_PIFV)();
    
    int _initterm_e(const _PIFV *ppfn, const _PIFV *end)
    {
        do 
        {
            if (_PIFV pfn = *++ppfn)
            {
                if (int err = pfn()) return err;
            }
        } while (ppfn < end);
    
        return 0;
    }
    

    so you can import it ot use own implementation. here faster question - what is use as input parameters ?

    say c++ (CL compiler how minimum) use ( ".CRT$XCA", ".CRT$XCZ" ) sections for place c++ global initializes. so we can do next:

    extern "C"
    {
    #pragma const_seg(".CRT$XCA")
      const _PVFV __xc_a = 0;
    #pragma const_seg(".CRT$XCZ")
      const _PVFV __xc_z = 0;
    
    #pragma const_seg(".CRT$XIA")
      const _PVFV __xi_a = 0;
    #pragma const_seg(".CRT$XIZ")
      const _PVFV __xi_z = 0;
    }
    
        /*
         * do initializations
         */
        initret = _initterm_e( &__xi_a, &__xi_z );
        if ( initret != 0 )
            return initret;
    
        /*
         * do C++ initializations
         */
        _initterm( &__xc_a, &__xc_z );
    

    but here need very good understand - what and why we doing