0

我正在使用 SDL 和 SDL_mixer 库,编译时出现以下错误:

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

当我使用 SDL_mixer 函数时,依此类推或所有实例。

我相当有信心错误出在 Makefile 中,因为它在我制作的另一个测试程序中编译得很好。

我的 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 */*~

错误在哪里?

4

2 回答 2

3

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.

于 2013-05-01T03:53:06.333 回答
1

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.

于 2013-05-01T03:45:21.707 回答