main 和 mainCRTStartup 有什么区别? [英] What is the difference between main and mainCRTStartup?

查看:26
本文介绍了main 和 mainCRTStartup 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解在 Microsoft 工具链中如何使用不同的入口点替换 WinMain.

I'm trying to understand how substituting a different entry point for WinMain works in the Microsoft toolchain.

我已经找到了这个问题,非常棒很有帮助,但最后一个细节一直困扰着我.

I already found this question and it was super helpful, but one last detail is nagging at me.

我第一次在 Visual Studio 中更改 Linker>Advanced>Entry Point 选项时,我错误地将其设置为 main 并且我的程序编译并运行良好.我后来意识到了这一点,并重新构建了程序,将其设置为 mainCRTStartup,正如链接问题中接受的答案所暗示的那样,并没有发现任何不同之处.

The first time I changed the Linker>Advanced>Entry Point option in Visual Studio, I set it to main by mistake and my program compiled and ran fine. I realized it later and rebuilt the program with it set to mainCRTStartup, as the accepted answer in the linked question suggests, and didn't find anything different.

那么,我的问题是:mainmainCRTStartup 之间有什么区别吗?如果有,有什么区别?

So, my question is: is there any difference at all between main and mainCRTStartup, and if so, what is the difference?

推荐答案

main() 是 C 或 C++ 程序的入口点.mainCRTStartup() 是 C 运行时库的入口点.它初始化 CRT,调用您在代码中编写的任何静态初始化程序,然后调用您的 main() 函数.

main() is the entrypoint of your C or C++ program. mainCRTStartup() is the entrypoint of the C runtime library. It initializes the CRT, calls any static initializers that you wrote in your code, then calls your main() function.

显然,首先执行 CRT 和您自己的初始化是必不可少的.如果没有发生这种情况,您可能会很难诊断错误.也许你不会,这是一个废话.您可以通过将此代码粘贴到一个小型 C++ 程序中来进行测试:

Clearly it is essential that both the CRT and your own initialization is performed first. You can suffer from pretty hard to diagnose bugs if that doesn't happen. Maybe you won't, it is a crap-shoot. Something you can test by pasting this code in a small C++ program:

class Foo {
public:
    Foo() {
        std::cout << "init done" << std::endl;
    }
} TestInit;

如果您将入口点更改为main",那么您将看到构造函数永远不会被调用.

If you change the entrypoint to "main" then you'll see that the constructor never gets called.

这很糟糕.

这篇关于main 和 mainCRTStartup 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆