简体   繁体   English

此代码是什么意思?

[英]what does this code mean?

this is some code that SDL requires in visual studios 2005 in order for my simple program to work. 这是SDL在visual studio 2005中需要的一些代码,以便我的简单程序能够正常工作。 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;
}

#pragma is a directive to the compiler. #pragma是编译器的指令。 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. 然后,它初始化SDL库。

Then it registers SDL_Quit function to be executed at program exit. 然后,它注册要在程序退出时执行的SDL_Quit函数。

Then pause, otherwise the program quits immediately. 然后暂停,否则程序立即退出。

Quick explanation: These lines: 快速说明:这些行:

#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif

are saying "If I'm being built on Windows, tell the linker to link with the SDL libraries." “如果我是在Windows上构建的,请告诉链接器链接到SDL库。”

Some background : When you compile your C program, it might not yet be complete. 一些背景 :编译C程序时,它可能尚未完成。 Other pieces of the final program might need to come from elsewhere - in your case, from the SDL libraries. 最终程序的其他部分可能需要来自其他地方-就您而言,来自SDL库。

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. #pragma comment(lib, ...)伪指令是一种告诉链接器您的代码需要哪些其他库才能成为完整程序的方法之一。

#pragma comment(lib, "SDL.lib")

This causes the linker to search for the library SDL.lib while linking. 这将导致链接器在链接时搜索库SDL.lib。 The second #pragma comment does the same for SDLmain.lib. 第二个#pragma comment对SDLmain.lib执行相同的操作。

This code: 这段代码:

#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif

The comment pragma is defined in the MSDN page . MSDN页面中定义了comment用法。 The lib argument means basically the same thing as specifying to dynamically link to the specified library: lib参数的含义与指定要动态链接到指定库的基本相同:

lib LIB

Places a library-search record in the object file. 将库搜索记录放置在目标文件中。 This comment type must be accompanied by a commentstring parameter containing the name (and possibly the path) of the library that you want the linker to search. 此注释类型必须带有一个注释字符串参数,该参数包含要链接器搜索的库的名称(可能还有路径)。 The library name follows the default library-search records in the object file; 库名称遵循目标文件中默认的库搜索记录; the linker searches for this library just as if you had named it on the command line provided that the library is not specified with /nodefaultlib . 如果未使用/ nodefaultlib指定该库,则链接程序将像在命令行上命名该库一样搜索该库。 You can place multiple library-search records in the same source file; 您可以在同一个源文件中放置多个库搜索记录。 each record appears in the object file in the same order in which it is encountered in the source file. 每条记录在源文件中的出现顺序与在源文件中遇到的顺序相同。

If the order of the default library and an added library is important, compiling with the /Zl switch will prevent the default library name from being placed in the object module. 如果默认库和添加的库的顺序很重要,则使用/ Zl开关进行编译将防止将默认库名放在对象模块中。 A second comment pragma then can be used to insert the name of the default library after the added library. 然后,可以使用第二个注释杂注在添加的库之后插入默认库的名称。 The libraries listed with these pragmas will appear in the object module in the same order they are found in the source code. 这些实用程序列出的库将以在源代码中找到的顺序出现在对象模块中。

adding to what Steffano mentioned... 添加到斯特凡诺提到的...

Basically, the code is checking to see if the SDL lib is available and able to initialize. 基本上,代码将检查SDL库是否可用并能够初始化。 If not you get the message. 如果没有,您会收到消息。 If it does initialize, it clears the intiialization through the atexit(). 如果确实初始化,它将通过atexit()清除初始化。

The code above main is setting pre-processor directives. main上面的代码是设置预处理程序指令。 From MS' description at ( http://msdn.microsoft.com/en-us/library/7f0aews7%28VS.80%29.aspx ): " 来自MS在( http://msdn.microsoft.com/zh-cn/library/7f0aews7%28VS.80%29.aspx )的描述:

Places a library-search record in the object file. 将库搜索记录放置在目标文件中。 This comment type must be accompanied by a commentstring parameter containing the name (and possibly the path) of the library that you want the linker to search. 此注释类型必须带有一个注释字符串参数,该参数包含要链接器搜索的库的名称(可能还有路径)。 The library name follows the default library-search records in the object file; 库名称遵循目标文件中默认的库搜索记录; the linker searches for this library just as if you had named it on the command line provided that the library is not specified with /nodefaultlib. 如果未使用/ nodefaultlib指定该库,则链接程序将像在命令行上命名该库一样搜索该库。 You can place multiple library-search records in the same source file; 您可以在同一个源文件中放置多个库搜索记录。 each record appears in the object file in the same order in which it is encountered in the source file. 每条记录在源文件中的出现顺序与在源文件中遇到的顺序相同。 If the order of the default library and an added library is important, compiling with the /Zl switch will prevent the default library name from being placed in the object module. 如果默认库和添加的库的顺序很重要,则使用/ Zl开关进行编译将防止将默认库名放在对象模块中。 A second comment pragma then can be used to insert the name of the default library after the added library. 然后,可以使用第二个注释杂注在添加的库之后插入默认库的名称。 The libraries listed with these pragmas will appear in the object module in the same order they are found in the source code." 这些实用程序列出的库将以在源代码中找到的顺序出现在对象模块中。”

The pragma stuff has been explained already. 实用说明已被解释。

"using namespace std" means that the compiler searches certain standard functions in the run time library (cout eg would actually be std::cout). “使用命名空间std”表示编译器在运行时库中搜索某些标准函数(例如,cout实际上是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 (eg 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". 现在,如果您在命名空间“ mystuff”中拥有自己的cout函数,则可以通过编写“ mystuff :: cout”将其与标准函数区分开。

The SDL call initializes the video and audio subsystems (eg looks whether there are video and audio devices available and whether they support all features SDL needs). SDL调用会初始化视频和音频子系统(例如,查看是否有可用的视频和音频设备,以及它们是否支持SDL所需的所有功能)。

"atexit (SDL_Quit)" means that the function "SDL_Quit" will automatically be called when your program terminates. “ atexit(SDL_Quit)”表示程序终止时将自动调用函数“ SDL_Quit”。

system ("pause") halts your program and waits for a keypress. 系统(“暂停”)会暂停程序并等待按键。

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

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