Jump to content

SDL Problems


Recommended Posts

I figure some programers here can help me. I wrote some C++ SDL programs and whenever I try to compile it reports all the SDL functions as being undefined. They are all spelt right and there no other problems with the program but, it still won't compile.

Link to comment
Share on other sites

Usually there are two packages for every library the library itself and a separate package that has the headers necessary to compile an app that uses that library they are usually suffixed with dev (as in development) so do you have both sdl and sdl-dev installed through your distro's package manager also IIRC there are several SDL libs so a function that you need may not be in the package you have installed.

I have SDL installed but, I'm referencing a copy of them in my home directory.

It is not recommended that you that as it will just confuse gcc as it looks in the standard locations first and a later reference muddies the water ;)

Enjoy the Choice :)

Edited by Loki1950
Link to comment
Share on other sites

Here's the code.


#include <stdio.h>
#include "usr/include/SDL/SDL.h"
SDL_Surface *screen;
SDL_Event event;
bool done = false;
int main(void)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("\"Hello, World!\"", "\"Hello, World!\"");
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
while(!done)
{
SDL_Flip(screen);
if(event.type == SDL_QUIT)
{
done = true;
}
}
SDL_Quit();
return 0;
}

This next one is from the manual.


#include "usr/include/SDL/SDL.h" /* All SDL App's need this */
#include <stdio.h>
int main() {

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
printf("Could not initialize SDL: %s.\n", SDL_GetError());
exit(-1);
}
printf("SDL initialized.\n");
printf("Quiting SDL.\n");

/* Shutdown all subsystems */
SDL_Quit();

printf("Quiting....\n");
exit(0);
}

Then the compiler output for the first one is


samgj@samgj:~/sdltest$ g++ sdltest.c -o sdltest
/tmp/ccaCl8C5.o: In function `main':
sdltest.c:(.text+0x11): undefined reference to `SDL_Init'
sdltest.c:(.text+0x25): undefined reference to `SDL_WM_SetCaption'
sdltest.c:(.text+0x49): undefined reference to `SDL_SetVideoMode'
sdltest.c:(.text+0x5d): undefined reference to `SDL_Flip'
sdltest.c:(.text+0x82): undefined reference to `SDL_Quit'
collect2: ld returned 1 exit status

But then this program compiles fine.


#include "usr/include/SDL/SDL.h"
#include <stdio.h>
int main
{
printf("Hello, World!");
return 0;
}

I use Debian 6.0.4 which had sdl and sdl-dev instaled automatically.

In my subdirectory I have the header files from the SDL website that's what '#include "usr/include/SDL/SDL.h"'

Link to comment
Share on other sites

Then the compiler output for the first one is


samgj@samgj:~/sdltest$ g++ sdltest.c -o sdltest
/tmp/ccaCl8C5.o: In function `main':
sdltest.c:(.text+0x11): undefined reference to `SDL_Init'
sdltest.c:(.text+0x25): undefined reference to `SDL_WM_SetCaption'
sdltest.c:(.text+0x49): undefined reference to `SDL_SetVideoMode'
sdltest.c:(.text+0x5d): undefined reference to `SDL_Flip'
sdltest.c:(.text+0x82): undefined reference to `SDL_Quit'
collect2: ld returned 1 exit status

You're not linking with any of the SDL libraries so the linker (ld) tells you they are undefined symbols. Including the headers in the source code only provides the definitions so that particular file can compile, but when it comes time for linking and creating the executable, you need to link with the required libraries - in this case SDL. You'll want to do something like this:


g++ sdltest.c -o sdltest `sdl-config --libs --cflags`

This will add the correct include and library paths, and the libraries needed to build an SDL application (e.g. SDL and SDLmain).

I suggest you read up on using the GNU toolchain ;)

Link to comment
Share on other sites

g++ sdltest.c -o sdltest `sdl-config --libs --cflags`

This doesn't work. It says No such file or directory.

Edit:

After I put #include <SDL/SDL.h> in the header and use accents rather than quotes it works.

Edited by samgj
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...