#include <SDL.h>
#include <iostream.h>

int main(int argc, char* argv[])
{
//initialize SDL and the video system.
if (SDL_Init( SDL_INIT_VIDEO)<0)
 return -1;
 
 //signal SDL to change the text of the main window to SDL "Hello World".
 SDL_WM_SetCaption("Hello world","Hello World");
 
 //Create an SDL_Surface object, which represents, the game window.
 SDL_Surface* screen=SDL_SetVideoMode(640,480,0,0);
 
 //Load the SDL logo bitmap to a temporary surface
 SDL_Surface* temp=SDL_LoadBMP("data\\structures\\sdl_logo.bmp");
 
 //Create the working SDL surface which matches the display format of the temporary surface.
 SDL_Surface* bg= SDL_DisplayFormat(temp);
 
 //Free the memory allocated to the temporary SDL_Surface.
 SDL_FreeSurface(temp);
 
 SDL_Event event;
 bool quit=false;
 
 //this is the main message loop if the game.
 while(!quit)
 {
   //Check message qeue for an event.
   if (SDL_PollEvent(&event))
   {
     //If an event was found.
     switch (event.type)
     {
       //check to see if the window was closed via the "x"
       case SDL_QUIT:
       //Set the quit flag to true
       quit=true;
       break;
       
      //Check the keyboard to see if the ESC key was pressed.
      case SDL_KEYDOWN:
       switch (event.key.keysym.sym)
       {
         case SDLK_ESCAPE:
           //set our quit flag to true
           quit=true;
          break;
       }
      break; 
     }
   } 
   //Draw the background sprite:
   SDL_BlitSurface(bg, NULL, screen, NULL);   
   
   //Update the current window.
   SDL_UpdateRect(screen,0,0,0,0);      
 }

//Free the allocated memery for the background surface.
SDL_FreeSurface(bg);

//Quit SDL and allow it to clean up everything.
SDL_Quit();

//return control to windows with no errors.
return 0;
}

But in the end my compiler comes with a lot of errors, all of them is:
[Linker error] undefined reference to `Something'
basically any command in the source code (Any SDL commands that is).
I have read in other threads, that it's because i have forgotten to include the library, but i think i have.
I'm using the dev-c++ compiler.
I went into the: Tools fan, and then chose: compiler options.
Then i chose Directories.
After that i went to the: "Directories" fan.
In here, there are three deifferend things i can include:
Binaries, Libraries, C includes and C++ includes.
I didn't include anything in binaries, since i couldn't find any SDL binaries.
I included the directory called: "Lib", as the SDL library.
I included the directory called: "Include" as both the C includes and the C++ includes, since i didn't know which of them it was supposed to be included as.
SDL, needs to be installed, and as so, it get's a stand alone directory, in C:/Program files/SDL (Or whereever you choose to locate it), the directorires i refer to, that i included, are all directories located in that directory.
So it seems, to me, like i included about everything, that's supposed to be included.
It should be said that i'm learning C++. so i'm a n00b at this. I'm new to both the langauge and the compiler, though i have programmed in another langauge, for pretty some while.
But since i'm new, i would ask you to now assume that i can anything, cause i realy can't. So please help me, step by step, i haven't even made the source code. It's from my stipendium.
O, and please tell me why i have to do what i have to do. I don't just wanna know how to fix it, i also wanna know why i'm fixing it the way i am, since i'm learning the langauge, i would also like to learn from this mistake ^^
i would also like to know how to correct the error, from the title. ([Linker error] undefined reference to `WinMain@16') i have came to the understanding, that, that specific error has another way to be corrected, than the rest.

>[Linker error] undefined reference to `WinMain@16'
You've created a project that's being built as a Win32 application. Those guys use WinMain instead of main as the entry point. If you want a regular C++ program, you have to choose the correct project type (which would be a console project of some sort) or build it yourself manually from the command line.

>but that didn't realy anwser my main question ^^
I answered the part that I'm qualified to answer. I don't use SDL, so I can't walk you through the basics of setting it up on your system. I'm sure their documentation tells you how to do it in detail, and you're getting errors because you neglected to follow the instructions.

(most) windows programs are written to run against an environment subsystem. the subsystem is specified in the PE header of the executable image and is placed there by the linker.
currently, your program is being linked with the linker switch /SUBSYSTEM:WINDOWS. this implies that your program does not require a console, probably because it creates its own windows for interaction with the user. if you do not specify an entry point for your executable (where the program should start executing) by using the /ENTRY linker switch, the linker inserts a suitable default. for /SUBSYSTEM:WINDOWS, this is WinMainCRTStartup (or wWinMainCRTStartup) which after initializing the C runtime library will call WinMain (or wWinMain).
since you are writing a console based program, you need to change the linker switch to /SUBSYSTEM:CONSOLE. this specifies that your program is a Win32 character-mode application and needs to be given given a console on startup. the defaul;t entry point would now be mainCRTStartup (or wmainCRTStartup) which after initializing the C runtime library will call main (or wmain).
to modify this linker option in the vc++8 ide: Project->Properties->Linker->System->Subsystem
for more information: http://msdn2.microsoft.com/en-us/library/fcc1zstk(vs.71).aspx
http://msdn2.microsoft.com/en-us/library/f9t8842e(VS.71).aspx
note: this is really not a c++ question, c++ only talks about main,

KK, ty ecery1, i solved the problem.
the problem was, that when i installed the game i wrote the installation adrress to be: 'c:/program files/SDL', but Vista installed it under: 'c:/programmer/SDL' (programmer is the danish name for program files). Then when i browsed for the library folder i opened programs, the place vista installed it, and added the folder. But the compiler chose to add it as 'Program files' and, the installed folder, not 'programmer', the real folder, so i had to change it manually.

But i do have another error now, or well... two.
The first is:
cannot find -lobjc
Id returned 1 exit status

>but that didn't realy anwser my main question ^^
I answered the part that I'm qualified to answer. I don't use SDL, so I can't walk you through the basics of setting it up on your system. I'm sure their documentation tells you how to do it in detail, and you're getting errors because you neglected to follow the instructions.

Well... ty for the help you offered me, still, i don't wanna sound ungrathefull... cause i'm realy not :P

Hello,

Sorry for the late response (new member.)

I've had the same problem when using Dev C++ and the SDL library.
I can't say for certain it's the right way but this method fixed the problem for me.

Just before your instantiation of function main place the following line of code:

#undef main

Hope this helps!

Regards,
Tom

Thanks Tom. It worked for me too! I'm a complete noob, and I get loads of errors that I can't understand.

This article has been dead for over six months. Start a new discussion instead.