C++ 为什么 SDL 定义了 main 宏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11976084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why SDL defines main macro?
提问by Tibi
After having some trouble setting up SDL, I found out that SDL defines a macro that replaces main:
在设置 SDL 遇到一些麻烦后,我发现 SDL 定义了一个宏来替换 main:
#define main SDL_main
// And then
extern C_LINKAGE int SDL_main(int argc, char *argv[]);
This can also create compilation errors, if the main function doesn't have the argc
and argv
parameters defined.
如果主函数没有定义argc
和argv
参数,这也会产生编译错误。
This macro gives me headaches just when I see it... Why does SDL need to redefine main? After some more searching, I found that some people #undef main
, and use it the normal way.
这个宏一看就头疼……为什么SDL需要重新定义main?经过一番搜索,我发现了一些人#undef main
,并以正常方式使用它。
So this is the question:why does SDL need to redefine main, what does it do? Are there any side effects to undefining it?
那么问题来了:为什么 SDL 需要重新定义 main,它有什么作用?取消定义是否有任何副作用?
One thing I noticed is that SDL redirects standard output and error to files (and I don't want this behavior), and this behavior stops if I undefine main.
我注意到的一件事是 SDL 将标准输出和错误重定向到文件(我不希望这种行为),如果我取消定义 main,这种行为就会停止。
回答by James McNellis
Per the SDL Windows FAQ:
You should be using
main()
instead ofWinMain()
even though you are creating a Windows application, because SDL provides a version ofWinMain()
which performs some SDL initialization before calling your main code.If for some reason you need to use
WinMain()
, take a look at the SDL source code insrc/main/win32/SDL_main.c
to see what kind of initialization you need to do in yourWinMain()
function so that SDL works properly.
即使您正在创建 Windows 应用程序,您也应该使用
main()
而不是WinMain()
,因为 SDL 提供了一个版本,WinMain()
它在调用您的主代码之前执行一些 SDL 初始化。如果由于某种原因需要使用
WinMain()
,请查看 中的 SDL 源代码,src/main/win32/SDL_main.c
了解您需要在WinMain()
函数中进行何种初始化,以便 SDL 正常工作。
SDL requires initialization, so it injects its own main
function that runs its initialization before calling your "main" function, which it renames to SDL_main
so that it does not conflict with the actual main
function. As noted in the FAQ, your main
function must be of the form
SDL 需要初始化,因此它会注入自己的main
函数,在调用您的“main”函数之前运行其初始化,它重命名为,SDL_main
以便它不会与实际main
函数冲突。如常见问题解答中所述,您的main
函数必须采用以下形式
int main(int argc, char* argv[])
回答by Mel
While I agree that it's a strange practice, there are situations where this is a reasonable solution, though it largely depends on the platform. Consider that different platforms have different entry points. Windows is typically WinMain, Linux is main, interacting with Android happens in Java, WinRT uses C++/CX extensions, and so on. Program entry point and APIs can be very platform specific and SDL tries to save you the trouble of having to deal with this. If you're only targeting Windows and SDL is only there to save you the trouble of working with WIN32 API, you might not need it. But if you ever go beyond desktop, you'll find it useful in my opinion.
虽然我同意这是一种奇怪的做法,但在某些情况下这是一个合理的解决方案,尽管它在很大程度上取决于平台。考虑到不同的平台有不同的入口点。Windows 通常是 WinMain,Linux 是主要的,与 Android 交互发生在 Java 中,WinRT 使用 C++/CX 扩展,等等。程序入口点和 API 可以是非常特定于平台的,SDL 试图为您省去处理这个问题的麻烦。如果您只针对 Windows 而 SDL 只是为了省去使用 WIN32 API 的麻烦,那么您可能不需要它。但是如果你超越了桌面,你会发现它在我看来很有用。