View Full Version : SDL trouble

12-23-2011, 10:49 AM
Hello there, Im quite new to OpenGL and SDL, I have some code from a friend which works 100% but when i paste it into a new program it doesnt find SDL,
Im using Fedora 15 Eclipse, i have OpenGL and SDL installed.
here is my code

#define GLEW_STATIC // Easier debugging
#include <GL/glew.h>
#include <GL/gl.h>
#include <SDL/SDL.h>
#include <iostream>
#include <fstream>

using namespace std;


* SDL timers run in separate threads. In the timer thread
* push an event onto the event queue. This event signifies
* to call display() from the thread in which the OpenGL
* context was created.
Uint32 display(Uint32 interval, void *param) {
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = RUN_GRAPHICS_DISPLAY;
event.user.data1 = 0;
event.user.data2 = 0;
return interval;

void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

// Don't forget to swap the buffers

int main(int argc, char ** argv) {
SDL_Surface * surf;
Uint32 width = 640;
Uint32 height = 480;
Uint32 colour_depth = 16; // in bits
Uint32 delay = 1000/60; // in milliseconds

// Initialise SDL - when using C/C++ it's common to have to
// initialise libraries by calling a function within them.
cout << "Failed to initialise SDL: " << SDL_GetError() << endl;

// When we close a window quit the SDL application

// Create a new window with an OpenGL surface
if (!(surf = SDL_SetVideoMode(width, height, colour_depth, SDL_OPENGL))) {
cout << "Failed to initialise video mode: " << SDL_GetError() << endl;

// Initialise GLEW - an easy way to ensure OpenGl 2.0+
// The *must* be done after we have set the video mode - otherwise we have no OpenGL context.
if (!glewIsSupported("GL_VERSION_2_0")) {
cerr<< "OpenGL 2.0 not available" << endl;
return 1;

// Call the function "display" every delay milliseconds
SDL_AddTimer(delay, display, NULL);

// Add the main event loop
SDL_Event event;
while (SDL_WaitEvent(&amp;event)) {
switch (event.type) {
case SDL_QUIT:

Error codes

**** Build of configuration Debug for project DurianSoftware-Chapter2 ****

make all
Building file: ../Main.cpp
Invoking: Cross G++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Main.d" -MT"Main.d" -o"Main.o" "../Main.cpp"
Finished building: ../Main.cpp

Building target: DurianSoftware-Chapter2
Invoking: Cross G++ Linker
g++ -o"DurianSoftware-Chapter2" ./Main.o
./Main.o: In function `display(unsigned int, void*)':
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:24: undefined reference to `SDL_PushEvent'
./Main.o: In function `display()':
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:29: undefined reference to `glClearColor'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:30: undefined reference to `glClear'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:33: undefined reference to `SDL_GL_SwapBuffers'
./Main.o: In function `main':
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:45: undefined reference to `SDL_Init'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:46: undefined reference to `SDL_GetError'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:47: undefined reference to `SDL_Quit'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:51: undefined reference to `SDL_Quit'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:54: undefined reference to `SDL_SetVideoMode'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:55: undefined reference to `SDL_GetError'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:56: undefined reference to `SDL_Quit'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:61: undefined reference to `glewInit'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:62: undefined reference to `glewIsSupported'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:68: undefined reference to `SDL_AddTimer'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:75: undefined reference to `SDL_Quit'
/home/ben/workspace/DurianSoftware-Chapter2/Debug/../Main.cpp:72: undefined reference to `SDL_WaitEvent'
collect2: ld returned 1 exit status
make: *** [DurianSoftware-Chapter2] Error 1

But i have a openGL program that draws 4 cubes and has the same main (nearly). I can give the code if you would like. but my question is how come my program doesnt find SDL???


P.S. i do also understand this is the openGL forums but i wanted to ask here first as i have more openGL questions to ask at a later date

12-23-2011, 01:31 PM

You need to tell compiler to use SDL and GL libraries (-lSDL -lGL flags).

12-23-2011, 02:30 PM
I understand that, but in Linux Fedora you dont have to, it just imports them in, and how come the other file works fine? i did the same with that :(

Cheers for reply tho

12-23-2011, 02:59 PM
On MinGW, Linux, etc you need to add BOTH compile and link flags, you get these flags from sdl-config, one way is to add the following to one's makefile:

CCFLAGS+= `sdl-config --cflags`
LDFLAGS+= `sdl-config --libs`

or something to that affect to add the bits from sdl-config to your compile and linking flags.

12-26-2011, 04:50 AM
Ok so how would make a makefile? and how can i link that to the main.cpp file and what not?

12-26-2011, 06:11 AM
Linker flags need to be setup to -lSDL -lOpengl32 and the libaries depending on your compiler if its gcc its .a file format of the libary if its anything else it could be .lib not shore about that part

12-26-2011, 07:49 AM
Fedora Eclipse generates the makefile. The likely best option is to navigate within Eclipse and add to your compile flags

`sdl-config --cflags`

and then add to your linker flags:

`sdl-config --libs`

if that does not work, then you'll do the following, at a terminal type the commands without the `:

#sdl-config --cflags

and copy paste that to the compile flags of Eclipse settings for your project and then paste the results of

#sdl-config --libs

to your linker flags of your project linker flags.