# Can't Compile With SDL: Mysterious Linker Errors

This topic is 3667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have been trying to use SDL to begin working on a game project that I want to run on Windows. Unfortunately it hasn't even got to the point of being called a game, because I am unable to get it to compile properly. I am using SDL 1.2.13, and I downloaded and installed MingW/MSYS so I can use 'make' and what have you. I'm Windows XP SP2. I've got all the library/include directories set up, and here is the code I am trying to compile:
#include <SDL/SDL.h>

int main ( int argc, char* argv [] )
{
if ( SDL_Init ( SDL_INIT_EVERYTHING ) )
return -1;

SDL_Surface* screen = SDL_SetVideoMode ( 640, 480, 0, SDL_SWSURFACE );
SDL_WM_SetCaption ( "Test", "Test" );

bool quitting = false;
while ( !quitting )
{
SDL_Event event;
while ( SDL_PollEvent ( &event ) )
{
//For now, just quit when a key is pressed.
if ( event.type == SDL_KEYUP || event.type == SDL_QUIT )
quitting = true;
}
}

return 0;
}

When I compile this, I use the following commands:
g++ -c main.cpp -o main.o
g++ -I"C:/SDL-1.2.13/include/" -L"C:/SDL-1.2.13/lib/" -lmingw32 -mwindows -lSDLmain -lSDL main.o  -o Example
But here's where it gets confusing. I get the following linker errors:
main.o:main.cpp:(.text+0x3f): undefined reference to SDL_SetVideoMode'

main.o:main.cpp:(.text+0x56): undefined reference to SDL_WM_SetCaption'

main.o:main.cpp:(.text+0x6b): undefined reference to SDL_PollEvent'
I have tried juggling the library switches, making sure everything is included properly and recompiling SDL. But I am at a loss, because it does not show an undefined reference to SDL_Init like I would expect if the library was not being linked all together. In fact, I am able to compile a very bare-bones program with just SDL_Init and run it successfully. Why am I unable to use other functions from the library, and how could I rectify this issue? It's like only half of libSDL is being linked. I've spent most of today trying to figure it out but nobody else seems to have had this problem. Thanks for any help in advance. Maybe I'll be able to work in earnest soon!

##### Share on other sites
If you are in windows, compiling for windows, why not use Microsoft Visual Studio 2008 Express? It's an excellent IDE (and also free)!

edit:
Anyways, you've specified where the compiler should look for includes and libs, but you haven't specified an actual library for it to link your project to.

erm, ... [smile]

I looked in the sdl's mingw libs, and the libraries are called "libSDL.la" and "libSDLmain.a". You're not trying to use to Visual C++'s "SDLmain.lib," are you? [smile]

[Edited by - _fastcall on June 6, 2008 9:45:19 PM]

##### Share on other sites
Why don't you look up Lazy Foo's SDL tutorials?
They are very good at covering this basic stuff.

##### Share on other sites
Try this.
g++ -I"C:/SDL-1.2.13/include/" -Wall -c main.cpp -o main.og++ -L"C:/SDL-1.2.13/lib/" -lmingw32 -mwindows -lSDLmain -lSDL main.o  -o Example

Is the -mwindows supposed to be in there?

##### Share on other sites
if you are using mingw, there is a nicer way to get the SDL libraries:

sdl-config --libs

you also need to get the compile falgs for SDL as well,

sdl-config --cflags

I do (in my makefile)

CFLAGS=sdl-config --cflags and whatever other flags I use
LIBS=sdl-config --libs and whatever other libs I need

then I compile with

$(CC)$(CFLAGS) -c foo.cpp (or foo.c)

$(CC)$(CFLAGS) -o Target $(OBJLIST)$(LIBS)

##### Share on other sites
Quote:
 Original post by kRogueif you are using mingw, there is a nicer way to get the SDL libraries:sdl-config --libsyou also need to get the compile falgs for SDL as well,sdl-config --cflagsI do (in my makefile)CFLAGS=sdl-config --cflags and whatever other flags I useLIBS=sdl-config --libs` and whatever other libs I needthen I compile with$(CC)$(CFLAGS) -c foo.cpp (or foo.c)and link with$(CC)$(CFLAGS) -o Target $(OBJLIST)$(LIBS)

Yup I've been doing a similar thing for Allegro now that I am messing with Linux again. I run allegro-config --libs and copy the output to the compiler link options inside of KDevelop for any of my Allegro programs.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631424
• Total Posts
3000008
×