Can't compile program that uses SDL after upgrade to 11.10 - undefined reference to SDL_Init

Solution 1:

Ok, solved. Apparently, for some mysterious reason, the order of the gcc options now matters. So when I do:

gcc -I /usr/include/SDL -o test test.cpp -lSDL

(moved the -lSDL option to the end) everthing works just fine. I'd love to know why it suddenly matters, when before if did not, but for now I'm happy that stuff works again.

Solution 2:

For Eclipse: I've same problem, but I resolve.

  • Select project
  • Project>Properties
  • C/C++ Build>Settings - Tool Settings>GCC C Compiler - Include paths (-l)>"/usr/include/SDL"
  • C/C++ Build>Settings - Tool Settings>GCC C Linker - Libraries (-l)>"SDL"
  • Apply

...then build project and run...

Solution 3:

A simple GNU Makefile for a project which uses SDL:

CXXFLAGS:=(shell pkg-config --cflags sdl2) $(CXXFLAGS)
LDLIBS:=$(shell pkg-config --libs sdl2) $(LDLIBS)

all: test

Where a file test.cpp exists in the same directory as the Makefile.

(Note that I've used sdl2 instead of sdl, since SDL 1.2 is basically dead now.)

GNU make will magically figure out the command for calling g++.