您当前的位置: 首页 >  c++

令狐掌门

暂无认证

  • 0浏览

    0关注

    513博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C++ main函数运行分析

令狐掌门 发布时间:2019-09-09 19:08:35 ,浏览量:0

           在VS2017中,建议一个C++ 控制台程序:

#include "stdafx.h"
#include 

int main()
{
	std::cout  wmain

//  * WinMainCRTStartup  => WinMain

//  * wWinMainCRTStartup => wWinMain

      我们是控制台程序,因此是main,  再搜索mainCRTStartup, 此时得在VC++目录搜搜:

     

          在文件的头部有如下描述

// The mainCRTStartup() entry point, linked into client executables that

// uses main().

         代码如下:

extern "C" int mainCRTStartup()
{
    return __scrt_common_main();
}

        mainCRTStartup是main函数最开始的调用位置,进入到 __scrt_common_main(), 可以查看源码

static __forceinline int __cdecl __scrt_common_main() throw()
{
    // The /GS security cookie must be initialized before any exception handling
    // targeting the current image is registered.  No function using exception
    // handling can be called in the current image until after this call:
    __security_init_cookie();

    return __scrt_common_main_seh();
}

            进入到__scrt_common_main_she()

static __declspec(noinline) int __cdecl __scrt_common_main_seh() throw()
{
    if (!__scrt_initialize_crt(__scrt_module_type::exe))
        __scrt_fastfail(FAST_FAIL_FATAL_APP_EXIT);

    bool has_cctor = false;
    __try
    {
        bool const is_nested = __scrt_acquire_startup_lock();

        if (__scrt_current_native_startup_state == __scrt_native_startup_state::initializing)
        {
            __scrt_fastfail(FAST_FAIL_FATAL_APP_EXIT);
        }
        else if (__scrt_current_native_startup_state == __scrt_native_startup_state::uninitialized)
        {
            __scrt_current_native_startup_state = __scrt_native_startup_state::initializing;

            if (_initterm_e(__xi_a, __xi_z) != 0)
                return 255;

            _initterm(__xc_a, __xc_z);

            __scrt_current_native_startup_state = __scrt_native_startup_state::initialized;
        }
        else
        {
            has_cctor = true;
        }

        __scrt_release_startup_lock(is_nested);

        // If this module has any dynamically initialized __declspec(thread)
        // variables, then we invoke their initialization for the primary thread
        // used to start the process:
        _tls_callback_type const* const tls_init_callback = __scrt_get_dyn_tls_init_callback();
        if (*tls_init_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_init_callback))
        {
            (*tls_init_callback)(nullptr, DLL_THREAD_ATTACH, nullptr);
        }

        // If this module has any thread-local destructors, register the
        // callback function with the Unified CRT to run on exit.
        _tls_callback_type const * const tls_dtor_callback = __scrt_get_dyn_tls_dtor_callback();
        if (*tls_dtor_callback != nullptr && __scrt_is_nonwritable_in_current_image(tls_dtor_callback))
        {
            _register_thread_local_exe_atexit_callback(*tls_dtor_callback);
        }

        //
        // Initialization is complete; invoke main...
        //

        int const main_result = invoke_main();

        //
        // main has returned; exit somehow...
        //

        if (!__scrt_is_managed_app())
            exit(main_result);

        if (!has_cctor)
            _cexit();

        // Finally, we terminate the CRT:
        __scrt_uninitialize_crt(true, false);
        return main_result;
    }
    __except (_seh_filter_exe(GetExceptionCode(), GetExceptionInformation()))
    {
        // Note:  We should never reach this except clause.
        int const main_result = GetExceptionCode();

        if (!__scrt_is_managed_app())
            _exit(main_result);

        if (!has_cctor)
            _c_exit();

        return main_result;
    }
}

         这里有段代码:

        这里是才调用我们写的main方法:

          进入main函数,我们可以启动调试

         打好断点,首先进入的是mainCRTStartup(), 依次执行,直到我们程序中的main()方法。这些过程都是编译器为我们执行的,在实际编写代码时可以不用关心这些,只需写我们的业务代码,算法等。

关注
打赏
1652240117
查看更多评论
0.0379s