error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

编译一段程序时,出现连接器错误:

error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

 

该程序大致如下:

 

VisualStudio 10.0

properies | general | character set : "Use Unicode Character Set"

properies | linker | system : "Console (SUBSYSTEM:CONSOLE)"

 

namespace mystock
{

const int .....;
const wchar_t* const .....;
class .......{};
class .......{};
.......

int _cdecl wmain(int argc, wchar_t* argv[])
{
    ......;
    ......;
}

} // namespace mystock

为什么?我的main函数看起来没错啊?

 

对于CRT在什么情况下会要求什么样的main函数,相信读者很清楚。可这次的问题从未遇到过。

 

干想了二十分钟,终于想起来了。或者说是蒙对了,其实心里仍然不很清楚。

我注意到了连接器想要的是“_main”,为什么有“_”?即使是向导生成的_tmain,在宏展开后也是main或者wmain啊?

 

自动加下划线,不是C编译器对cdecl的修饰规则吗?对了,难道是因为我把wmain放到一个namespace里,就怎么怎么着了?

 

那就咬牙试一试:

extern "C" int _cdecl wmain(int argc, wchar_t* argv[]){ ......

 

连接器错误消失。

 

如果读者你,恰好也遇到类似问题,希望这篇文章有所帮助。

如果你知道的更详细,请回复指导:

1,为什么main函数放到namespace里就必须要extern "C"了?

2,main不放到namespace里,但是在.cpp文件里,在编译器默认设置的情况下,难道就是extern "C"了?不应该啊。

posted @ 2013-06-08 20:59  爱生活,爱编程  阅读(9063)  评论(0编辑  收藏  举报