将 WinMain 参数传递给另一个函数
我正在 Windows 上开发一个应用程序,但我也想支持其他平台(我编写的大部分代码都是平台无关的)。不管怎样,我认为为了开发,我想让事情变得简单(也许我对这种方法很天真),所以我的 main.cpp
看起来有点像下面这样:
#ifdef _WIN32
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR cmdLine,
int cmdShow)
#else
int main(int argc, char* argv[])
#endif
{
/* If the application is launched under Windows */
#ifdef _WIN32
win_init wi;
return wi.init(hInstance, hPrevInstance, cmdLine, cmdShow);
#endif
/* If the application is launched under OS X */
#ifdef __APPLE__
osx_init oi;
return oi.init();
#endif
}
所以我的想法是,如果应用程序在 Windows 下启动,调用 WinMain
,然后我使用提供的 WinMain 参数调用 wi.init 来完成其余工作。
我以为我很聪明,但是当我编译时出现错误:
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
我研究了这个错误并且当人们忘记主要方法或尝试将 DLL 编译为 .exe 时,似乎就会发生这种情况
所以对于我的问题;我很好奇为什么会发生这种情况,您建议我做什么来代替这种方法?
I'm developing an application on Windows, but I'd also like to support other platforms (the majority of code I've written is platform independent). Anyway, I figured for developments sake I'd like to keep things simple (perhaps I was naive in this approach) so my main.cpp
looks a little like the following:
#ifdef _WIN32
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR cmdLine,
int cmdShow)
#else
int main(int argc, char* argv[])
#endif
{
/* If the application is launched under Windows */
#ifdef _WIN32
win_init wi;
return wi.init(hInstance, hPrevInstance, cmdLine, cmdShow);
#endif
/* If the application is launched under OS X */
#ifdef __APPLE__
osx_init oi;
return oi.init();
#endif
}
So the idea is that if the application is launching under Windows, WinMain
is called and then I call wi.init with the WinMain arguments provided to do the rest.
I thought I was being smart, but when I compile I get an error:
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
I've researched this error and it seems to happen when people either forget a main method or try to compile a DLL as an .exe
So for my question; I am curious as to why this is happening, and what do you recommend I do instead of this approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您需要使用
/SUBSYSTEM:WINDOWS
链接器选项才能使用WinMain
。You want to use the
/SUBSYSTEM:WINDOWS
linker option in order forWinMain
to be used.