Archived

This topic is now archived and is closed to further replies.

scpedicini

weird SDL problem

Recommended Posts

I am trying to use the SDL library with Mingw g++ (GCC) 3.2.3 I downloaded the latest package of SDL and extracted the Header files to an include\SDL subdirectory and copied the four lib files: libSDL.a, libSDL.dll.a, libSDLmain.a, libSDL.la into my lib folder. I compile everything straight from a DOS box in Windows XP using: g++ -lmingw32 -lSDLmain -lSDL -mwindows universe.cpp -o universe But I get the error: undefined reference to SDL_SetVideoMode However, the previous line that uses SDL_Init causes no problems, if I delete the SDL_SetVideoMode part, then the program compiles and links okay. I''ve tried switching around the g++ command parameters in every possible combination with no success. Here is the code for the program, pretty simple:
#include "sdl.h"

int main(int argc, char *argv[])
{
	SDL_Surface *Screen, *Image;

	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);

	Screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);

	SDL_Quit();
	return 0;
}

Does anybody have any insight into this? I have no idea what I''m doing wrong. Thanks, Shaun

Share this post


Link to post
Share on other sites
-lSDL needs to be before -lSDLmain

#include "sdl.h"
should be
#include <sdl.h> //It should be in your include folder

Not sure if these things will fix your problem though.

Share this post


Link to post
Share on other sites
whoops,

the line: #include "sdl.h" was supposed to be:
#include <SDL/SDL.H> as the header files are in
subdirectory include\sdl

I tried putting the -lSDL before lSDLmain but the compiler
reported:

d:/mingw/bin/../lib/gcc-lib/mingw32/3.2.3/../../../libSDLmain.a(SDL_win32_main.o
.b)(.text+0x2c9): In function `console_main'':
/home/hercules/public_cvs/SDL12/src/main/win32/SDL_win32_main.c:219: undefined r
eference to `SDL_Init''
d:/mingw/bin/../lib/gcc-lib/mingw32/3.2.3/../../../libSDLmain.a(SDL_win32_main.o
.b)(.text+0x2d8):/home/hercules/public_cvs/SDL12/src/main/win32/SDL_win32_main.c
:256: undefined reference to `SDL_GetError''
d:/mingw/bin/../lib/gcc-lib/mingw32/3.2.3/../../../libSDLmain.a(SDL_win32_main.o
.b)(.text+0x334):/home/hercules/public_cvs/SDL12/src/main/win32/SDL_win32_main.c
:224: undefined reference to `SDL_Quit''
d:/mingw/bin/../lib/gcc-lib/mingw32/3.2.3/../../../libSDLmain.a(SDL_win32_main.o
.b)(.text+0x34b):/home/hercules/public_cvs/SDL12/src/main/win32/SDL_win32_main.c
:244: undefined reference to `SDL_SetModuleHandle''
C:\DOCUME~1\shaun\LOCALS~1\Temp/ccSYaaaa.o(.text+0xc):universe.cpp: undefined re
ference to `SDL_Init''
C:\DOCUME~1\shaun\LOCALS~1\Temp/ccSYaaaa.o(.text+0x25):universe.cpp: undefined r
eference to `SDL_SetVideoMode''
C:\DOCUME~1\shaun\LOCALS~1\Temp/ccSYaaaa.o(.text+0x30):universe.cpp: undefined r
eference to `SDL_Quit''

Thanks for the help though,
Shaun

Share this post


Link to post
Share on other sites
quote:
Original post by MetaCipher
#include "sdl.h"
should be
#include <sdl.h>
quote:
Original post by scpedicini
the line: #include "sdl.h" was supposed to be:
#include <SDL/SDL.H> as the header files are in
subdirectory include\sdl
SDL FAQ: Do I #include <SDL.h> or <SDL/SDL.h>

The nature of your error (undefined reference) indicates that it is a linker error, not a compiler error, so it''s not likely to be a source code problem since SDL_SetVideoMode is defined elsewhere. I notice that the unpacked files you listed included an "SDL.dll.a"; is that a typo? The problem obviously lies in your environment setup.

Share this post


Link to post
Share on other sites
Get an IDE. Its better to compile from there than compiling from a DOS box.

Anyways SDL.dll.a isn''t a file.

You should have the following:
libSDL.a, libSDLmain.a, and SDL.dll.

When using the mingw compiler you should link to the libraries in a certain other like so:

-lmingw32
-lSDLmain
-lSDL

SDL should be in /include/SDL/here

My typical SDL program

#include <stdio.h>
#include <stdlib.h>

#include <SDL/SDL.h>

int SDL_main( int argc, char ** argv )
{

SDL_Init( SDL_INIT_VIDEO );

SDL_Surface * screen = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );

if( screen == NULL )
{
fprintf( stderr, "%s", SDL_GetError() );
return EXIT_FAILURE;
}

SDL_Quit();
return EXIT_SUCCESS;
}


If this doesn''t work then you''ve set the library up wrong...



-----------------------
"In reality God is a comedian but his audience are too afraid to laugh."

Share this post


Link to post
Share on other sites
I thought for cross compatibility sake, you add a -i(insert SDLheader directory here)
and include "SDL.h" instead.. that''s what''s worked for me this whole time, and takes minimal effort to port afterwards.. And minimal effort is a good thing

Share this post


Link to post
Share on other sites
I''m getting problems similar to the original poster.

I''m compiling under cygwin & minGW

Everything that comes through SDL.h is fine and is linked correctly in the library (to the SDL.c I believe), I can initialize cd-rom drivers, video drivers etc. and SDL_Quit no problem.

when I try to referece anything from another .h file, e.g. SDL_cdrom.h - corresponding to SDL_cdrom.c, such as ''SDL_CDNumDrives()'' - a fairly standard function, I get ''undefined reference to `SDL_CDNumDrives'' - or whatever thing I tried to reference. when I try and compile.

I can only guess the library libSDL.a hasn''t linked SDL_cdrom.c correctly. unfortunately, I don''t know gcc very well, and dont have a good understand of libraries and linking etc. I figure somethings not good with my build of the libSDL.a file, but I cant understand the makefile well enough to figure out whats going on.

----

#dont worry about the pre-path bit, I need to use it for some odd reason.
pre-path = c:/cygwin
input-file = sdlTest2.cc
libs = -L${pre-path}/usr/local/lib -lmingw32 -lSDLmain -lSDL -mwindows -mno-cygwin
incl =

all:
gcc -Wall -I/usr/local/include/SDL -Dmain=SDL_main -I${pre-path}/usr/include/mingw -DWIN32 -Uunix
... -L${pre-path}/usr/local/lib -lmingw32 -lSDLmain -lSDL -mwindows -mno-cygwin
... sdlTest2.cc

#include "SDL.h"
#include <stdlib.h>
int main(int argc, char *argv[]) {

printf("Initializing SDL.\n");
if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_CDROM) < 0 ) {
printf("Error: Could not initialize SDL: %s.\n", SDL_GetError());
exit(-1);
}
else{
printf( "Got the CDrom drivers intitialized in SDL!\n");
}
printf("SDL initialized.\n");
printf("CD drive(s): available: %i\n", SDL_CDNumDrives());
atexit(SDL_Quit);
return(0);
};

any ideas?

Share this post


Link to post
Share on other sites
Sorry, forgot to mention, the only error I get is

sdlTest2.cc: undefined reference to `SDL_CDNumDrives''

If I take out the SDL_CDNumDrives() line then it compiles (and runs) fine.

It''s not just SDL_CDNumDrives, its any function or variable from SDL_cdrom.h - and SDL_audio.h and SDL_video.h for that matter. I''ve been hacking round in the libsdl.a file and theres definite mention of the SDL_CDNumDrives function. I can''t understand why it doesnt link. I''ve also tried explicitly linking the src/cdrom/.libs/libcdrom.a library explicitly, and that does nothing as well.

this is obviously the same prob as the other guy had must be a solution somewhere...

Share this post


Link to post
Share on other sites
I am aware I''ve been talking to myself these last 3 posts, but to confound matters further, an ''nm'' on the libsdl.a to reveal the symbol tables shows that everything that should be there is in there- in particular


SDL_cdrom.o:
00000000 b .bss
00000000 d .data
00000000 i .drectve
00000000 N .stab
00000000 N .stabstr
00000000 t .text
00000080 t _CheckInit
00000710 T _SDL_CDClose
000006d0 T _SDL_CDEject
00000140 T _SDL_CDName
*** 000000f0 T _SDL_CDNumDrives *** <-- this is the one
000001a0 T _SDL_CDOpen
000005b0 T _SDL_CDPause
00000560 T _SDL_CDPlay
etc..

so CDNumDrives *is* linked in there.

do I need to activate the SDL_cdrom.o somehow or something? I''m not sure how I would do that...

Share this post


Link to post
Share on other sites
Finally figured out the solution. Here it is for anyone else who gets in a similar mess.

The order you put libraries, inputs etc. is important to gcc, and counter-intuitively perhaps in gcc if an object(or library) references another object(or library), then it needs to appear *before* the object.

So the order for including -lSDL -lSDLmain and {sourceFile} has to be:

-lSDLmain {sourcefile} -lSDL

so
1.sourcefile comes before -lSDL to use the stuff inside it.
2. -lSDLmain comes before sourcefile in order to link to the SDL_main function in it (main gets renamed to SDL_main - trust me)

its a bit confusing when you do sdl-config --libs and see -lSDLmain -lSDL, since your source has to actually go between the two.

although I guess you could actually you could do someting like

libs = `sdl-config --libs''
${libs} {source} -lSDL

okay. hope someone else benefits off that, its been a long time coming.

Share this post


Link to post
Share on other sites