Linking to SDL2 in Ubuntu 10

Here is my makefile:

CC=g++

CFLAGS=-c -O3 -Wall sdl2-config --cflags
LDFLAGS=sdl2-config --libs -L/usr/local/lib -lGL -lSDL2main -lSDL2_ttf
OTHERFLAGS=-g
SOURCES=all/my/source.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=my_executable
all: $(SOURCES) $(EXECUTABLE)
clean-object:
rm -f *.o
clean-executable:
rm -f $(EXECUTABLE);
clean: clean-object clean-executable
rebuild: clean all
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OTHERFLAGS) $(LDFLAGS) $(OBJECTS) -o $@
.cpp.o:
$(CC) $(OTHERFLAGS) $(CFLAGS) $< -o $@

So, everything compiles fine except, I get a pile of Undefined reference
SDL_*

sdl2-config --libsd --cflags gives me this output (which appears to be
correct):

-L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lpthread

-I/usr/local/include/SDL2 -D_REENTRANT

Any ideas? I haven’t played with SDL2 on Ubuntu before (only mingw and
msvc++)
-Alex

2012/5/17 Alex Barry <alex.barry at gmail.com>

So, everything compiles fine except, I get a pile of Undefined reference
SDL_*

sdl2-config --libsd --cflags gives me this output (which appears to be
correct):

-L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lpthread

-I/usr/local/include/SDL2 -D_REENTRANT

Any ideas? I haven’t played with SDL2 on Ubuntu before (only mingw and
msvc++)
-Alex

It depends on the GCC version (which in turn depends on the Ubuntu version
you are using), the exact specifics escape me right now, but assuming your
library path is right, it looks like you are being bitten by the dependency
resolver “feature” in newer versions of GCC…(at least from Ubuntu 11.04
onwards)
Basically, if libSDL2_ttf uses a symbol from libSDL2, the order in which
you list them in the command line is very important as the linker solves
dependencies in that order…The first thing I’d try is swapping the line
like this:

LDFLAGS=-L/usr/local/lib -lSDL2main -lSDL2_ttf sdl2-config --libs -lGL

Hopefully that solves it.–
Gabriel.