Search code examples
c++makefilesdlsdl-mixer

Linking errors with SDL_mixer library


I'm working with the SDL and SDL_mixer library and am getting the following errors when I compile:

....
game.cpp:(.text+0x88f): undefined reference to `Mix_OpenAudio'
Jukebox.o: In function `Jukebox::~Jukebox()':
Jukebox.cpp:(.text+0x17): undefined reference to `Mix_FreeChunk'
Jukebox.cpp:(.text+0x27): undefined reference to `Mix_FreeChunk'
Jukebox.cpp:(.text+0x37): undefined reference to `Mix_FreeChunk'
Jukebox.cpp:(.text+0x47): undefined reference to `Mix_FreeChunk'
....

And so on and so forth or all instances when I use a SDL_mixer function.

I'm fairly confident that the error lies within the Makefile because it compiles just fine in another test program I made.

My Makefile

SDL= -lSDL -lSDL_mixer

OBJ=game.o Jukebox.o ...

all:    main

main:   $(OBJ)
        g++ $(SDL) $(OBJ) -o main

%.o:    %.cpp
        g++ $(SDL) -c $<

clean:
        rm -f *.o *~ main
        rm -f */*~

Where is the error?


Solution

  • I think the problem is the order of your arguments.

    Instead of

    main:   $(OBJ)
            g++ $(SDL) $(OBJ) -o main
    

    try

    main:   $(OBJ)
            g++ -o main $(OBJ) $(SDL) 
    

    While the position of -o main is not really important, the order of the link libraries is. Compilers resolve the symbols in the order the libraries appear on the command line.