VC修改了入口主函数,全局对象不构造问题?

FrankSun80 FrankSun80 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
问题描述:VC2013新建Win32 Console Application,全部代码如下,

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class CWidget
{
public:
CWidget()
{
_tprintf(_T("widget()\n"));
}
~CWidget()
{
_tprintf(_T("~widget()\n"));
}
};

CWidget widget;

int _tmain(int argc, _TCHAR* argv[])
{
_tprintf(_T("Hello World.\n"));
return 0;
}


编译运行,一切正常。运行结果为:
widget()
Hello World.
~widget()
Press any key to continue . . .


接下来修改项目属性,Linker-> Advanced-> Entry Point 设为 my_main,并对应的修改主函数名,修改后的代码如下,

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class CWidget
{
public:
CWidget()
{
_tprintf(_T("widget()\n"));
}
~CWidget()
{
_tprintf(_T("~widget()\n"));
}
};

CWidget widget;

int my_main(int argc, _TCHAR* argv[])
{
_tprintf(_T("Hello World.\n"));
return 0;
}


编译,链接,有警告如下:
1>ConsoleApplication2.obj : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>MSVCRTD.lib(atonexit.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators

运行结果为:
Hello World.
Press any key to continue . . .

可以看到,全局对象的构造函数和析构函数都没有调用。那么问题来了,如果我想修改主函数的名称,同时又希望全局对象像以前正常构造析构,该怎么做呢?
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
FrankSun80
C++ 语言 65,212 成员 250,513 内容