SDL: Can't compile hello world

Started by
27 comments, last by nerman 17 years, 8 months ago
This is a simple thing. I took a hello world which compiled fine and then added the Sdl.h include into it and tried to compile,... Code: //------------ // code start //------------ // Purpose: Your standard Hello World for C++ #include<iostream> using namespace std; #include"SDL\Sdl.h" int main(void) { std::cout << "Hello World"; return 0; } //------ // end code //------- I get this from the compiler: C:\C++\Mingw32\i386-mingw32\lib\libmingw32.a(main.o)(.text+0x8e): undefined reference to `WinMain@16' I think if I can get past this I'll be doing ok.
BladeStoneOwner, WolfCrown.com
Advertisement
Make sure you include the libs: mingw32, SDL and SDLmain
Another nameless person in the virtual space...
Your problem is the definition of your main function. SDL expects:

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


Also, are you aware that the standard stream is re-routed to a file and will not display on the screen? You need to get a text library of some description, or create your own. Your current code will save "Hello World!" to a file called SDLout or something like that. It will show a black console window (not an SDL surface even as you don't have it being set up) and will quit. Actually, since no surface is defined, I'm not 100% sure that your text will be re-routed right away, I think you have to init SDL first... But I haven't included it without initializing it before so I don't know for sure.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
First off, I'm not to keen on my library vers headers but I looked for all the files in the directorys for my C++ and no luck.

There is a SDL.h, and a SDL_main.h
There was no Mingw32 anything

I believe my compiler that I'm using is Mingw32. I've added the SDL.h and SDL_main.h into the includes,... but not luck, same problem.
BladeStoneOwner, WolfCrown.com
On an unrelated matter, once you compile that it will only be on the screen for a fraction of a second as the program runs to fast for you to read.

Add 'system("PAUSE");' or 'cin.get()' to the end of your code right before 'return 0;'

     ~S of the L~


You need to include the .libs in your linker or parameters. Therein lies your problem.

What is the name of your compiler? Not 'mingw32'. It should say in the title bar right above 'file' 'edit' and those type of tabs. Heres a few:

'Microsoft visual basic'
'Dev C++'
'Code::blocks'
'Angel Code'
and others...

Recongize any?
Quote:Original post by M2tM
Your problem is the definition of your main function. SDL expects:

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


Also, are you aware that the standard stream is re-routed to a file and will not display on the screen? You need to get a text library of some description, or create your own. Your current code will save "Hello World!" to a file called SDLout or something like that. It will show a black console window (not an SDL surface even as you don't have it being set up) and will quit. Actually, since no surface is defined, I'm not 100% sure that your text will be re-routed right away, I think you have to init SDL first... But I haven't included it without initializing it before so I don't know for sure.


I don't know what kind of IDE you are using but I'm one step up from notepad for my coding, and I do command-line compiles. Which, if it succeeds, it creates and <filename>.exe which the <filename>.exe works fine.

So my code does look like this now with no change.

//--------------
// Purpose: Your standard Hello World for C++

#include "SDL\SDL.h"
#include "SDL\SDL_main.h"

#include<iostream>

using namespace std;

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

std::cout << "Hello World";

return 0;
}
BladeStoneOwner, WolfCrown.com
Quote:Original post by BladeStone
First off, I'm not to keen on my library vers headers but I looked for all the files in the directorys for my C++ and no luck.

There is a SDL.h, and a SDL_main.h
There was no Mingw32 anything

I believe my compiler that I'm using is Mingw32. I've added the SDL.h and SDL_main.h into the includes,... but not luck, same problem.


What NamelessTwo was saying is as follows:

1. Click "Project" -> "Project Options"

2. Click on the "Parameters" tab at the top

3. In the "Linker" box, add in (in this order):
-lmingw32
-lsdlmain
-lsdl

4. Recompile and it should work fine, make sure that your main function is what M2tM posted: int main(int argc, char *argv[])

EDIT: Now that I see you are not using DevCpp, you will need to link in those libraries in step 3:

libmingw32.a libsdlmain.a libsdl.a
Try:

#include <iostream>using namespace std;int main(){   cout << "\tHello world!\n";   system("PAUSE");   return 0;}


and then give us the errors word for word.
Servant, I know you mean well, but the code snippet needs to have the main definition I gave earlier. And it's a linker error at this point, not a code error.

Actually, if all went well, the problem should be resolved because of the advice already supplied. If he is having futher issues, I would suggest one of the following:

1. make sure you have all the SDL files required.
2. make sure your dll is visible to your program.
3. make sure you have your main declared as I pointed out.
4. make sure you are including all the correct libs.

And that should have it working.

I'd suggest getting a free IDE too, they really make your life easier.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
[quote
On an unrelated matter, once you compile that it will only be on the screen for a fraction of a second as the program runs to fast for you to read.

Add 'system("PAUSE");' or 'cin.get()' to the end of your code right before 'return 0;'

     ~S of the L~


You need to include the .libs in your linker or parameters. Therein lies your problem.

What is the name of your compiler? Not 'mingw32'. It should say in the title bar right above 'file' 'edit' and those type of tabs. Heres a few:

'Microsoft visual basic'
'Dev C++'
'Code::blocks'
'Angel Code'
and others...

Recongize any?

A few comments:
If you run the file from dos prompt I get the Hello World to be writen to the next line and the command line prompt follows that, so I've not had a problem with that, but I'll add it ait any ways.

I hate to be the 3 year newbie here, but I know nothing about my linker, I'll look into that.

Finally, I got my little IDE from a CD from a Standard C++ Bible book and the IDE is Quincy 99. I only use it to write some of my code in and move from file to file with in, but nothing more then that
BladeStoneOwner, WolfCrown.com

This topic is closed to new replies.

Advertisement