[SDL] Re: SDL_mixer SDL_image and SDL_ttf problems

Benjamin Deutsch ben at fictiongroup.de
Tue Jan 25 13:44:29 PST 2005

>>Re: SDL_mixer SDL_image and SDL_ttf problems
>>You have to add the libraries like so:
>>gcc blah blah sdl-config -lSDL_image -lSDL_ttf -lSDL_mixer
>>because sdl-config only includes the base SDL library.
> Thanks, I tried it but this is what happened :
> g++ TroutWars.cpp -o TrWars1 `/usr/bin/sdl-config --libs` -lSDL_image
> -lSDL_ttf -lSDL_mixer
> /usr/lib/libSDL_ttf.a(SDL_ttf.o)(.text+0x67): In function `TTF_Init': :
> undefined reference to `FT_Init_FreeType'

"Undefined reference" generally means the linker knows something exists, 
but cannot find it to add to the executable. In this case, the function 
FT_Init_FreeType is used, directly or indirectly, by your program, but 
the linker, ld (which g++ calls), does not know which library this 
function is part of.

Since FT_Init_FreeType is a FreeType function (which SDL_ttf inferfaces 
with), add -lfreetype to your list of libraries. More general tips follow.

If a library has an unresolved symbol, try calling "ldd" on its dynamic 
linkable version. In this case, libSDL_ttf.a is complaining about an 
undefined reference. Calling "ldd /usr/lib/libSDL_ttf.so" (.so are 
dynamic linkable libraries) will give you a list of libraries it needs, 
and where it is currently finding them.

When in doubt, add these libraries to your compilation call. 
libSDL_ttf.so depends (on my system) on libfreetype.so.6, so I replace 
the "lib" part with "-l", ignore the ending and the version number, and 
arrive at "-lfreetype". This tells my compiler/linker to also check the 
freetype library.

One thing about libraries and link order: each library is usually added 
in the order in which it appears on the command line. Each library is 
scanned for any symbols which, until now, have been unresolved. These 
new symbols may refer to further undefined symbols (i.e. your code 
referred to TTF_OpenFont, which in turn needs FT_Open_Face, etc.). These 
new symbols are only searched in libraries further on the command line!

Normally, you don't need to worry about the entire previous paragraph, 
but if you're sure you've added the correct libraries, and there are 
still undefined references, you may have added them in the wrong order. 
Try working out the order with the above paragraph (generally: first the 
derived libraries, then the basic libraries), or just trying things out.

Have fun!

   Benjamin Deutsch

More information about the SDL mailing list