简体   繁体   English

extern int main(int argc, char* argv[]) 使用?

[英]extern int main(int argc, char* argv[]) use?

I've been reading SFML's source code.我一直在阅读 SFML 的源代码。 I found the way it wraps the win32 in a fashion like this:我发现它以如下方式包装 win32 的方式:

#ifdef _WIN32 //something like that
#include <windows.h>
extern int main(int argc, char* argv[]);
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
   return main(__argc, __argv);
}
#endif

so line 4 I see this typical win32 program entry.所以line 4我看到了这个典型的 win32 程序入口。 however what are the extern main and return main(__argc, __argv) doing?但是extern mainreturn main(__argc, __argv)在做什么?

what's __argc with the underscore?带下划线的__argc是什么?

as in my own main function after loading SFML, all I need is to write int main() .就像我在加载 SFML 后我自己的主要 function 一样,我只需要编写int main()即可。

I am very curious how this work in terms of writing cross-platform codes.我很好奇这在编写跨平台代码方面是如何工作的。 (I used the same fashion in my win32 code, it worked?? anyone explain the magic behind this please???) (我在我的 win32 代码中使用了相同的方式,它有效??有人解释一下这背后的魔力吗???)

What are the extern main and return main(__argc, __argv) doing? extern mainreturn main(__argc, __argv)在做什么?

If you are compiling on a windows platform, SFML defines the WinMain entry point for you, and calls your main(int argc, char* argv[]) with __argc and __argv which are explained by the relevant documentation :如果您在 windows 平台上编译,SFML 为您定义 WinMain 入口点,并使用__argc__argv调用您的main(int argc, char* argv[])相关文档对此进行了解释:

The __argc global variable is a count of the number of command-line arguments passed to the program. __argc全局变量是传递给程序的命令行 arguments 的数量的计数。 __argv is a pointer to an array of single-byte-character or multi-byte-character strings that contain the program arguments. __argv是指向包含程序 arguments 的单字节字符或多字节字符串数组的指针。

SFML does this, exacly such that that developers can use the standard main function even in a Win32 Application project, and thus keep a portable code. SFML 这样做是为了让开发人员即使在 Win32 应用程序项目中也可以使用标准的主 function,从而保持可移植代码。 The comments in SFML/MainWin32.cpp explain this. SFML/MainWin32.cpp中的注释解释了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM