这段代码是什么意思?
这是 SDL 在 Visual Studios 2005 中需要的一些代码,以便我的简单程序能够运行。代码在做什么?我拥有它的唯一原因是我的教练告诉我把它放进去但从未解释过。
// what is this code doing?
//---------------------------------------------------------
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
//-------------------------------------------------------
#include <iostream>
#include "SDL.h"
using namespace std;
int main(int argc, char *argv[])
{
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1) {
cerr << "Failed to initialize SDL: " << SDL_GetError() << endl;
exit(1);
}
atexit(SDL_Quit);
system("pause");
return 0;
}
this is some code that SDL requires in visual studios 2005 in order for my simple program to work. what is the code doing? the only reason i have it is because my instructor told me to put it in and never explained it.
// what is this code doing?
//---------------------------------------------------------
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
//-------------------------------------------------------
#include <iostream>
#include "SDL.h"
using namespace std;
int main(int argc, char *argv[])
{
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1) {
cerr << "Failed to initialize SDL: " << SDL_GetError() << endl;
exit(1);
}
atexit(SDL_Quit);
system("pause");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
#pragma
是编译器的指令。在这种情况下,它要求编译器将“注释”放入最终的目标文件中,然后链接器使用该注释来链接库。然后它初始化 SDL 库。
然后它注册SDL_Quit函数以在程序退出时执行。
然后暂停,否则程序立即退出。
#pragma
is a directive to the compiler. In this case, it asks the compiler to put a "comment" into the final object file, and this comment is then used by the linker to link against the library.Then it initializes the SDL library.
Then it registers SDL_Quit function to be executed at program exit.
Then pause, otherwise the program quits immediately.
快速解释:这些行:
说“如果我是在 Windows 上构建的,请告诉链接器与 SDL 库链接。”
一些背景 strong>:当您编译 C 程序时,它可能尚未完成。最终程序的其他部分可能需要来自其他地方 - 在您的情况下,来自 SDL 库。
链接器是一个软件,它将您的代码与其他库结合起来以生成最终的程序。
#pragma comment(lib, ...)
指令是告诉链接器您的代码需要哪些其他库才能成为完整程序的方法之一。Quick explanation: These lines:
are saying "If I'm being built on Windows, tell the linker to link with the SDL libraries."
Some background: When you compile your C program, it might not yet be complete. Other pieces of the final program might need to come from elsewhere - in your case, from the SDL libraries.
The linker is a piece of software that combines your code with those other libraries to produce the finished program. The
#pragma comment(lib, ...)
directive is one of the ways of telling the linker which other libraries your code needs in order to become a complete program.此代码:
comment
pragma 在 MSDN 页面。 lib 参数的含义与指定动态链接到指定库的含义基本相同:This code:
The
comment
pragma is defined in the MSDN page. Thelib
argument means basically the same thing as specifying to dynamically link to the specified library:这会导致链接器在链接时搜索库 SDL.lib。第二个
#pragma comment
对 SDLmain.lib 执行相同的操作。This causes the linker to search for the library SDL.lib while linking. The second
#pragma comment
does the same for SDLmain.lib.添加 Steffano 提到的内容...
基本上,代码正在检查 SDL 库是否可用并能够初始化。如果没有,您会收到消息。如果它确实初始化,它会通过 atexit() 清除初始化。
adding to what Steffano mentioned...
Basically, the code is checking to see if the SDL lib is available and able to initialize. If not you get the message. If it does initialize, it clears the intiialization through the atexit().
上面的代码主要是设置预处理器指令。来自 MS 的描述 ( http://msdn. microsoft.com/en-us/library/7f0aews7%28VS.80%29.aspx ): "
The code above main is setting pre-processor directives. From MS' description at ( http://msdn.microsoft.com/en-us/library/7f0aews7%28VS.80%29.aspx ): "
编译指示的内容已经解释过了。
“使用命名空间std”意味着编译器在运行时库中搜索某些标准函数(cout 例如实际上是std::cout)。背景是您可以将命名空间中的符号分组,然后命名空间是符号的前缀。这允许您通过将相同的符号(例如函数名称)放在不同的名称空间中来使用它们。 “using namespace”指令意味着自动使用指定的命名空间为符号添加前缀。现在,如果您在命名空间“mystuff”中拥有自己的 cout 函数,则可以通过编写“mystuff::cout”将其与标准函数区分开来。
SDL 调用初始化视频和音频子系统(例如,查看是否有可用的视频和音频设备以及它们是否支持SDL 需要的所有功能)。
“atexit (SDL_Quit)”表示当程序终止时将自动调用函数“SDL_Quit”。
系统(“暂停”)暂停您的程序并等待按键。
The pragma stuff has been explained already.
"using namespace std" means that the compiler searches certain standard functions in the run time library (cout e.g. would actually be std::cout). The background is that you can group symbols in namespaces which are then a prefix of the symbol. This allows you to use identical symbols (e.g. function names) by putting them in different name spaces. The "using namespace" directive means to automatically prefix symbols with the namespace specified. Now if you have your own cout function from a namespace "mystuff" you can distinguish it from the standard one by writing "mystuff::cout".
The SDL call initializes the video and audio subsystems (e.g. looks whether there are video and audio devices available and whether they support all features SDL needs).
"atexit (SDL_Quit)" means that the function "SDL_Quit" will automatically be called when your program terminates.
system ("pause") halts your program and waits for a keypress.