我在链接SDL库时遇到问题

时间:2019-06-05 12:57:37

标签: c++ makefile sdl

我正在为我的2D沙箱游戏创建一个makefile,当我编译最终的可执行文件时,链接器无法识别所有SDL函数。

./ libs->所有SDL库(SDL2.lib,SDL2test.lib,SDL2_ttf.lib) ./include->所有SDL类和函数

#variables
SRC_DIR = .
LIBS = -Llib/SDL2 -Llib/SDL2main -Llib/SDL2test -Llib/SDL2_ttf
INC = -Iinclude
BIN = theGame
CXX = g++
CXXFLAGS = -pedantic -Wall
RM = rm

#make
theGame: main.o Block.o Character.o FileAccess.o GameObject.o Item.o Ladder.o NPC.o Player.o Window.o World.o
    $(CXX) $(CXXFLAGS) -o $(BIN) main.o Block.o Character.o FileAccess.o GameObject.o Item.o Ladder.o NPC.o Player.o Window.o World.o $(LIBS)

main.o: main.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

Block.o: Block.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

Character.o: Character.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

FileAccess.o: FileAccess.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

GameObject.o: GameObject.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

Item.o: Item.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

Ladder.o: Ladder.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

NPC.o: NPC.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

Player.o: Player.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

Window.o: Window.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

World.o: World.cpp
    $(CXX) -c $(CXXFLAGS) $(INC) $(SRC_DIR)/$^

  

g ++ -pedantic -Wall -o thegame main.o Block.o Character.o FileAccess.o   GameObject.o Item.o Ladder.o NPC.o Player.o Window.o World.o   -Llib / SDL2 -Llib / SDL2main -Llib / SDL2test -Llib / SDL2_ttf main.o:在函数pollMenuEvents(Window&)': main.cpp:(.text+0x23): undefined reference to SDL_PollEvent'main.o中:在函数pollEvents(Window&, std::vector<Character*, std::allocator<Character*> >&, World*)': main.cpp:(.text+0xae): undefined reference to SDL_PollEvent'Block.o中:   在函数Block::draw(int, int) const': Block.cpp:(.text+0xf4): undefined reference to SDL_SetRenderDrawColor'中   Block.cpp :(。text + 0x10b):对SDL_RenderFillRect' Ladder.o: In function Ladder :: draw(int,int)const'的未定义引用:   Ladder.cpp :(。text + 0x19a):未定义的引用   SDL_SetRenderDrawColor' Ladder.cpp:(.text+0x1b1): undefined reference to SDL_RenderFillRect'Ladder.cpp :(。text + 0x22e):未定义参考   到SDL_SetRenderDrawColor' Ladder.cpp:(.text+0x245): undefined reference to SDL_RenderFillRect'Ladder.cpp :(。text + 0x2dd):未定义   引用SDL_SetRenderDrawColor' Ladder.cpp:(.text+0x2f4): undefined reference to SDL_RenderFillRect'NPC.o:函数中   NPC::draw(int, int) const': NPC.cpp:(.text+0xbd0): undefined reference to SDL_SetRenderDrawColor'NPC.cpp :(。text + 0xbe7):未定义   引用“ SDL_RenderFillRect”

。 。

1 个答案:

答案 0 :(得分:0)

更改:

LIBS = -Llib/SDL2 -Llib/SDL2main -Llib/SDL2test -Llib/SDL2_ttf

收件人:

LIBS = -Llib -lSDL2 -lSDL2main -lSDL2test -lSDL2_ttf

编译器规则并不完全正确,因为前提条件中缺少源文件目录(它们只能工作,因为SRC_DIR.)。

所有这些编译器规则都可以替换为一个模式规则:

%.o: $(SRC_DIR)/%.cpp
    $(CXX) -o $@ -c $(CXXFLAGS) $(INC) $^