SDL Proplem

while i am compiling the code below it pops:
unresolved external:SDL Symbol RWReadFile referenced in function SDL_main
what is that mean and how can i fix this i am using visual studio
check the code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <SDL.h>

int main( int argc, char* args[] ){
	SDL_Surface* hello=NULL;
	SDL_Surface* screen=NULL;
	screen=SDL_SetVideoMode(640,400,32,SDL_SWSURFACE);
	hello=SDL_LoadBMP("hello.bmp");
	 SDL_BlitSurface( hello, NULL, screen, NULL );

    //Update Screen
    SDL_Flip( screen );

    //Pause
    SDL_Delay( 2000 );
	 //Free the loaded image
    SDL_FreeSurface( hello );

    //Quit SDL
    SDL_Quit();
	delete hello,screen;
    return 0;
}
Last edited on
There is a common issue with the SDL_main function that generates

Error LNK2019: unresolved external symbol _SDL_main referenced in function _main

I'm not sure if it's related in this case. However, there is a definition in SDL_main.h (which is included by SDL.h) that goes like this:

 
#define main SDL_main 


Try defining your main() as int main(int argc, char **argv) (to match SDL's) and see if it's resolved.
Last edited on
still not working l...i will list the errors for you:

Error 1 error LNK2019: unresolved external symbol _SDL_RWFromFile referenced in function _SDL_main C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\ConsoleApplication8\Source.obj ConsoleApplication8
Error 2 error LNK2019: unresolved external symbol _SDL_SetVideoMode referenced in function _SDL_main C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\ConsoleApplication8\Source.obj ConsoleApplication8
Error 3 error LNK2019: unresolved external symbol _SDL_Flip referenced in function _SDL_main C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\ConsoleApplication8\Source.obj ConsoleApplication8
Error 4 error LNK2019: unresolved external symbol _SDL_FreeSurface referenced in function _SDL_main C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\ConsoleApplication8\Source.obj ConsoleApplication8
Error 5 error LNK2019: unresolved external symbol _SDL_LoadBMP_RW referenced in function _SDL_main C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\ConsoleApplication8\Source.obj ConsoleApplication8
Error 6 error LNK2019: unresolved external symbol _SDL_UpperBlit referenced in function _SDL_main C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\ConsoleApplication8\Source.obj ConsoleApplication8
Error 7 error LNK2019: unresolved external symbol _SDL_Delay referenced in function _SDL_main C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\ConsoleApplication8\Source.obj ConsoleApplication8
Error 8 error LNK2019: unresolved external symbol _SDL_Quit referenced in function _SDL_main C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\ConsoleApplication8\Source.obj ConsoleApplication8
Error 9 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\ConsoleApplication8\MSVCRTD.lib(crtexe.obj) ConsoleApplication8
Error 10 error LNK1120: 9 unresolved externals C:\Users\mohammed\documents\visual studio 2012\Projects\ConsoleApplication8\Debug\ConsoleApplication8.exe ConsoleApplication8
I see. For what it's worth, another approach I've read was to

1
2
#include <SDL.h>
#undef main 


(although I am certainly unsure of the consequences of #undef-ing main! I mention it because I've just come across it in discussions involving SDL_main).

However, I now got a chance to refer to some old SDL code of mine (which did not generate errors) and I see that I have pretty much this structure in it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

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

// SDL Initializatation
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) 
    {
        /*exit with SDL_Init error*/
    }

bool running = true;
SDL_Event Event;

//Application main loop
while (running)
{
    while(SDL_PollEvent(&Event))
       {
	running = OnEvent(&Event);	
       }
}
	
// Quit SDL and close program

SDL_Quit();

	return 0;
}

//---------------------

bool OnEvent(SDL_Event* Event)
{
bool Running = true;
if(Event->type == SDL_QUIT) 
    { //Close Window event
	Running = false;
    }
	return Running;
}


---
EDIT:

By the way, I realized that you are not calling SDL_Init() in your program above. It should be called at the very beginning of the SDL program.


Last edited on
one weird error
1>c:\users\mohammed\documents\visual studio 2012\consoleapplication1\consoleapplication1\source.cpp(21): error C3861: 'OnEvent': identifier not found

You must declare OnEvent before main.
Examples:

1
2
3
4
5
6
7
bool OnEvent(SDL_Event * Event);
int main(int argc, char** argv) {
   // ...
}
bool OnEvent(SDL_Event * Event) {
   // ...
}


Or also:

1
2
3
4
5
6
bool OnEvent(SDL_Event * Event) {
   // ...
}
int main(int argc, char** argv) {
   // ...
}
Last edited on
Yes, of course -- like EssGeEich says, OnEvent() has to be declared before main() for this to compile, since it is a function written by myself and not part of SDL itself, but rather a function meant to handle SDL_Events.

I posted the code above as a figurative example put together by parts of my code in order to show the overall structure, I didn't mean it as an explicit compilable one. However, if you declare OnEvent() before main(), it will actually compile.
i think the errors are because there are include and libs file missing .... the brand new errors are another unresolved externals...i am sorry guys...i got you annoyed...the last time ...just give me an entire code and i will copy paste and see what is going on.....thanks for your trying
Well you must link to SDL library.
If you use Microsoft's Visual Studio compiler, you can:

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

Otherwise you must add SDL.lib in your IDE manually.
Also you must add SDL's directory in your IDE no matter which way you choose.
You must also
#pragma comment(lib, "SDLmain.lib")

Make sure that you have set these settings in your project:

http://lazyfoo.net/SDL_tutorials/lesson01/windows/msvsnet2010e/index.php

Also: my non-compilable example code above uses SDL_image but SDL_image is not part of the standalone SDL, it is an additional header file that allows some greater flexibility requiring image file types, etc. . So -- sorry to have gotten you confused, at the time of writing I supposed that it was apparent that the code was portraying structure and was not readily compilable.

If your project is set up properly and your additional dependencies and other settings are correct, this should compile fine:
1
2
3
4
5
6
7
8
9
10
11
#include <SDL.h>
 int main( int argc, char* args[] ) 
{ 
    //Initialize SDL 
    SDL_Init( SDL_INIT_EVERYTHING ); 
    
    //Quit SDL 
    SDL_Quit(); 
    
    return 0; 
}


If this doesn't compile, there's something left to be added in your project settings.

Try it out.

EDIT:

SDL requires SDL.lib and SDLmain.lib to be added in Project Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies (or requires using the #pragma comment commands for "SDL.lib" and "SDLmain.lib").

Furthermore, make sure that SDL's include directory is added in:
Project Properties -> Configuration Properties -> VC++ Directories -> Include Directories

and also add SDL's lib directory in Project Properties -> Configuration Properties -> VC++ Directories -> Library Directories. Steps 3-8 in the tutorial I linked for setting up SDL in Visual Studio describe this process.
Last edited on
thank you
Ogoyant,
EssGeEich
these two was all i want:
1
2
#pragma comment(lib,"SDL.lib")
#pragma comment(lib,"SDLmain.lib") 

now it is working fine and i can continue on SDL tutorials
Great, glad to hear you got it working Clearner1

Ogoyant
thanks for your care
Topic archived. No new replies allowed.