TheFuzz
TheFuzz

Reputation: 2623

what does this code mean?

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;
}

Upvotes: 0

Views: 1329

Answers (7)

user156676
user156676

Reputation:

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

Upvotes: 2

Razzupaltuff
Razzupaltuff

Reputation: 2301

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.

Upvotes: 0

Stefano Borini
Stefano Borini

Reputation: 143795

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

Upvotes: 9

atk
atk

Reputation: 9324

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

Upvotes: 0

Badfish
Badfish

Reputation:

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

Upvotes: 0

1800 INFORMATION
1800 INFORMATION

Reputation: 135295

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.

Upvotes: 2

RichieHindle
RichieHindle

Reputation: 281515

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.

Upvotes: 7

Related Questions