这段代码是什么意思?

发布于 2024-08-02 13:14:16 字数 689 浏览 6 评论 0原文

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情深已缘浅 2024-08-09 13:14:16

#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.

指尖上得阳光 2024-08-09 13:14:16

快速解释:这些行:

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

说“如果我是在 Windows 上构建的,请告诉链接器与 SDL 库链接。”

一些背景 strong>:当您编译 C 程序时,它可能尚未完成。最终程序的其他部分可能需要来自其他地方 - 在您的情况下,来自 SDL 库。

链接器是一个软件,它将您的代码与其他库结合起来以生成最终的程序。 #pragma comment(lib, ...) 指令是告诉链接器您的代码需要哪些其他库才能成为完整程序的方法之一。

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."

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.

末蓝 2024-08-09 13:14:16

此代码:

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

comment pragma 在 MSDN 页面。 lib 参数的含义与指定动态链接到指定库的含义基本相同:

将库搜索记录放入目标文件中。此评论类型
必须附有注释字符串
包含名称的参数(和
可能是库的路径)
您希望链接器进行搜索。这
库名称遵循默认值
对象中的库检索记录
文件;链接器搜索这个
库就像你已经命名它一样
命令行前提是
库未指定
/nodefaultlib 。您可以放置​​多个
同一图书馆检索记录
源文件;每条记录出现在
目标文件中的顺序相同
在源代码中遇到的
文件。

如果默认库和添加库的顺序是
重要的是,使用 /Zl 进行编译
开关将阻止默认设置
库名称被放置在
对象模块。第二个注释杂注
然后可用于插入名称
添加后的默认库
图书馆。列出的库
这些编译指示将出现在
对象模块的顺序相同
可以在源代码中找到。

This code:

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

The comment pragma is defined in the MSDN page. The lib argument means basically the same thing as specifying to dynamically link to the specified library:

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 . 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. 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.

潇烟暮雨 2024-08-09 13:14:16
#pragma comment(lib, "SDL.lib")

这会导致链接器在链接时搜索库 SDL.lib。第二个#pragma comment 对 SDLmain.lib 执行相同的操作。

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

This causes the linker to search for the library SDL.lib while linking. The second #pragma comment does the same for SDLmain.lib.

怪我鬧 2024-08-09 13:14:16

添加 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().

尛丟丟 2024-08-09 13:14:16

上面的代码主要是设置预处理器指令。来自 MS 的描述 ( http://msdn. microsoft.com/en-us/library/7f0aews7%28VS.80%29.aspx ): "

将库搜索记录放入目标文件中。此注释类型必须附有包含名称的注释字符串参数您希望链接器搜索的库的名称(可能还有路径) 链接器搜索该库时遵循目标文件中的默认库搜索记录,就像您在提供的命令行中命名它一样。未使用 /nodefaultlib 指定库。您可以将多个库搜索记录放入同一源文件中;每个记录按照在源文件中出现的顺序出现在目标文件中。

如果默认库和添加的库的顺序很重要,则使用 /Zl 开关进行编译将阻止将默认库名称放置在目标模块中。然后可以使用第二个注释编译指示在添加的库之后插入默认库的名称。使用这些编译指示列出的库将按照它们在源代码中找到的顺序出现在目标模块中。”

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 ): "

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. 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. 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."

撧情箌佬 2024-08-09 13:14:16

编译指示的内容已经解释过了。

“使用命名空间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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文