samgj Posted August 5, 2012 Report Share Posted August 5, 2012 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. Quote Link to comment Share on other sites More sharing options...
Loki1950 Posted August 5, 2012 Report Share Posted August 5, 2012 If you are compiling under Linux with gcc do you have the SDL headers installed usually sdl-dev package?Enjoy the Choice Quote Link to comment Share on other sites More sharing options...
samgj Posted August 5, 2012 Author Report Share Posted August 5, 2012 I have SDL installed but, I'm referencing a copy of them in my home directory. Quote Link to comment Share on other sites More sharing options...
Loki1950 Posted August 5, 2012 Report Share Posted August 5, 2012 (edited) 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 August 5, 2012 by Loki1950 Quote Link to comment Share on other sites More sharing options...
historic_bruno Posted August 6, 2012 Report Share Posted August 6, 2012 If you built SDL yourself, use the sdl-config that resulted (usually in [prefix]/bin) to get the cflags and ldflags you need to compile. Although you'd probably need to do a "make install" for that. Or you can use the sdl-config that came with libsdl-dev from your distro's package manager. Quote Link to comment Share on other sites More sharing options...
samgj Posted August 6, 2012 Author Report Share Posted August 6, 2012 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 issamgj@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 statusBut 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"' Quote Link to comment Share on other sites More sharing options...
historic_bruno Posted August 6, 2012 Report Share Posted August 6, 2012 Then the compiler output for the first one issamgj@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 statusYou'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 Quote Link to comment Share on other sites More sharing options...
samgj Posted August 7, 2012 Author Report Share Posted August 7, 2012 Thanks. Quote Link to comment Share on other sites More sharing options...
samgj Posted August 8, 2012 Author Report Share Posted August 8, 2012 (edited) 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 August 20, 2012 by samgj Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.