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++
.
Related videos on Youtube
adam
Updated on September 18, 2022Comments
-
adam almost 2 years
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
andlibsdl1.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?
-
Oxwivi over 12 yearsMaybe you should check the release notes for the version of
gcc
in 11.10. -
Eelke about 12 yearsThanks, 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`
-
qwr over 7 yearsRead about why here stackoverflow.com/questions/9417169/…