# Quick question in SDL

## Recommended Posts

Ok, so I'm just now starting to learn SDL...and I think I over prepared for it dramatically. Now considering this is the first API that I've tried to use with C++, I'm having some obvious issues. I'm pretty sure that I have my project set up correctly. I've been trying to learn through the LazyFoo tutorials, and the first one pretty much showed me that I've set it up correctly. I've double checked and the linking settings are set up right, everything. I have the updated version of SDL.dll in my system32 folder (which tested out right in the first tutorial) and after this doing the second one and failing, I even put another copy in the project folder. Sorry, I'm babbling. Let my make this a little more simple. I have a 2 linker errors in my project (tutorial #2 from Lazy Foo's SDL tutorials) and I would like to know what I'm doing wrong. I'm not going to bother posting the source code, as you can see it from the website if you wish, not to mention this isn't a syntax error it is a linker error, but I will post the build output. 1>------ Build started: Project: SDL Tut 2, Configuration: Debug Win32 ------ 1>Linking... 1>SDLmain.lib(SDL_win32_main.obj) : error LNK2019: unresolved external symbol _SDL_main referenced in function _main 1>C:\Documents and Settings\jd-inflames\My Documents\Visual Studio 2008\Projects\SDL Tuts\SDL Tut 2\Debug\SDL Tut 2.exe : fatal error LNK1120: 1 unresolved externals 1>Build log was saved at "file://c:\Documents and Settings\jd-inflames\My Documents\Visual Studio 2008\Projects\SDL Tuts\SDL Tut 2\SDL Tut 2\Debug\BuildLog.htm" 1>SDL Tut 2 - 2 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

##### Share on other sites
The SDL_main.lib provides a main for you, that calls SDL_main, you have to provide it a SDL_main function. Either change your main to SDL_main, or don't link to SDL_main.lib.

##### Share on other sites
Make sure your main function is defined as:

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

Make sure you are linking against SDL.lib and SDLmain.lib

Edit: Fixed the wrong version of "your" above ;)

##### Share on other sites
Another note -- take SDL.dll out of your System32 directory -- you don't want it to be globally accessible to windows. Should you run any other program that requires SDL and it uses a different version than what you put in system32 it won't work right. Windows checks system32 first, then the program directory.

Move it instead to your program directory (if you're running out of your debug/release directories also put a copy in there).

##### Share on other sites
Ok, I've tried both renaming main() to SDLmain() and then trying removing SDLmain.lib. Either method produces the same error.

As for SDL.dll being in my system32 folder, this is on a development machine of mine. The only programs it runs are programs that I write. That being said, the event of using a different version of SDL is rather controlled.

##### Share on other sites
as ontheheap said:

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

##### Share on other sites
Ok, I realized issue one was that I was using args() instead of args[]. Kind of a silly mistake, but it was simple enough that it explains why I overlooked it. It compiles now with no error, but now I'm getting run time errors. I'll go ahead and post the source to make things easier to look at in case I made another stupid boo boo like the above.

//The Headers#include "SDL.h"#include <string>//The attributes of the screenconst int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;//The surfaces that will be usedSDL_Surface *message = NULL;SDL_Surface *background = NULL;SDL_Surface *screen = NULL;SDL_Surface *load_image(std::string filename){	//Temporary storage for the image that's loaded	SDL_Surface* loadedImage = NULL;	//The optimized image that will be used	SDL_Surface* optimizedImage = NULL;	//Load the image	loadedImage = SDL_LoadBMP(filename.c_str());	//If nothing went wrong in loading the image	if (loadedImage != NULL)	{		//Create an optimized image		optimizedImage = SDL_DisplayFormat(loadedImage);		//Free the old image		SDL_FreeSurface(loadedImage);	}	//Return the optimized image	return optimizedImage;}void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination){	//Make a temporary rectangle to hold the offsets	SDL_Rect offset;	//Give the offsets to the rectangle	offset.x = x;	offset.y = y;	//Blit the surface	SDL_BlitSurface(source, NULL, destination, &offset);}int main(int argc, char* args[]){	//Initialize all SDL subsystems	if (SDL_Init(SDL_INIT_EVERYTHING) == -1)	{		return 1;	}	//Set up the screen	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);	//If there was an error in setting up the screen	if (screen = NULL)	{		return 1;	}	//Set up the window caption	SDL_WM_SetCaption("Hello World", NULL);	//Load the images	message = load_image("hello_world.bmp");	background = load_image("background.bmp");	//Apply the background to the screen	apply_surface(0, 0, background, screen);	//Apply the message to the screen	apply_surface(180, 140, message, screen);	//Update the screen	if (SDL_Flip(screen) == -1)	{		return 1;	}	//Wait 2 seconds	SDL_Delay(2000);	//Free the surfaces	SDL_FreeSurface(message);	SDL_FreeSurface(background);	//Quit SDL	SDL_Quit();	//Return	return 0;}

##### Share on other sites
//Set up the screenscreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);// if (screen = NULL)   // <--- Whoops, this assigns NULL to screen, then evaluates the result (NULL is 0, which is always false)if (screen == NULL)    // <--- This checks if screen is NULL{	return 1;}//Set up the window captionSDL_WM_SetCaption("Hello World", NULL);//Load the imagesmessage = load_image("hello_world.bmp");background = load_image("background.bmp");// Check to see if your images are loading correctly here; you don't want// to spend any time looking at your apply_surface function or the SDL_Flip// function wondering why your images aren't being displayed  :)if ( message == NULL || background == NULL )  {	return 1;}/*...*///Free the surfacesSDL_FreeSurface(message);SDL_FreeSurface(background);SDL_FreeSurface(screen);  // <--- Don't forget this!

##### Share on other sites
SDL_FreeSurface(screen);  // <--- Don't forget this!

Actually, that has no effect. I really don't remember where I saw that in the first place, I had to lurk into SDL source to find out: (snippet of SDL_surface.c)
/* * Free a surface created by the above function. */void SDL_FreeSurface (SDL_Surface *surface){	/* Free anything that's not NULL, and not the screen surface */	if ((surface == NULL) ||	    (current_video &&	    ((surface == SDL_ShadowSurface)||(surface == SDL_VideoSurface)))) {		return;	}	...}

To really free the screen surface, you have to call SDL_Quit(). That is really something not to forget about. This function will call SDL_VideoQuit(), which will actually free the screen surface.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627722
• Total Posts
2978804

• 10
• 9
• 21
• 14
• 12