为什么SDL定义主宏? [英] Why SDL defines main macro?

查看:218
本文介绍了为什么SDL定义主宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置SDL之后遇到一些问题,我发现SDL定义了一个替换main的宏:

After having some trouble setting up SDL, I found out that SDL defines a macro that replaces main:

#define main SDL_main

// And then
extern C_LINKAGE int SDL_main(int argc, char *argv[]);

这也可以创建编译错误,如果main函数没有 argc argv 定义参数。

This can also create compilation errors, if the main function doesn't have the argc and argv parameters defined.

看到它...为什么SDL需要重新定义主?经过一些更多的搜索,我发现有些人 #undef main ,并以正常的方式使用。

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.

strong>所以这是问题: 为什么SDL需要重新定义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将标准输出和错误重定向到文件(我不想要行为),如果我未定义主要行为,则此行​​为停止。

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.

推荐答案

Per SDL Windows常见问题


您应该使用 main()而不是 WinMain(),即使您正在创建Windows应用程序,因为SDL提供了一个版本 WinMain(),在调用主代码之前执行一些SDL初始化。

You should be using main() instead of WinMain() even though you are creating a Windows application, because SDL provides a version of WinMain() which performs some SDL initialization before calling your main code.

某些原因你需要使用 WinMain(),看一下SDL源代码在 src / main / win32 / SDL_main.c 以查看你的 WinMain()函数中需要做什么样的初始化,以使SDL正常工作。

If for some reason you need to use WinMain(), take a look at the SDL source code in src/main/win32/SDL_main.c to see what kind of initialization you need to do in your WinMain() function so that SDL works properly.

SDL需要初始化,因此它会注入自己的 main 函数,在调用main函数之前,重命名为 SDL_main ,以使其不与实际的 main 函数冲突。如常见问题所述,您的 main 函数必须采用以下形式:

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

int main(int argc, char* argv[])

这篇关于为什么SDL定义主宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆