简体   繁体   中英

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?

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.

It seems that you linker cannot find where the libraries are located. Identify where they were installed and pass this path to the linker via -L directive.

Put something like this: SDL= -L/path/to/installed/SDL/libraries -lSDL -lSDL_mixer

Note that, in: g++ $(SDL) -c $< the variable $(SDL) is irrelevant, once your are not linking into your program, but just generating the objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM