8

I've just upgraded from 11.4 to 11.10 and at least one thing seems to be broken: I have a C++ program that is using SDL and OpenGL. I was able to compile and run it just fine before the upgrade. Now I get linker errors, for example

undefined reference to `SDL_Init'

I have libsdl1.2debian and libsdl1.2-dev packages installed, I believe that is all I need (but I might be wrong). I compile the program like this:

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

Any idea what can be causing this?

0

4 Answers 4

18

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.

3
  • Maybe you should check the release notes for the version of gcc in 11.10.
    – Oxwivi
    Oct 19, 2011 at 14:56
  • 2
    Thanks, very helpfull. You can also completly turn around the order to gcc -o test test.cpp -I /usr/include/SDL -lSDL which is useful when you use pkg-config as you can then still get the cflags and libs in one go like gcc -o test test.cpp `pkg-config --cflags --libs sdl`
    – Eelke
    Jun 15, 2012 at 4:51
  • 2
    Read about why here stackoverflow.com/questions/9417169/…
    – qwr
    Feb 8, 2017 at 4:49
1

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...

1

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++.

0

In my case, I was using G++ and it didn't work, and switching to GCC it works. I am writing in C though.

Using Code::Blocks, the library flag is -lSDL2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .