gpt4 book ai didi

c++ - 我在链接 SDL 库时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:43 26 4
gpt4 key购买 nike

我正在为我的 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: In function pollMenuEvents(Window&)': main.cpp:(.text+0x23): undefined
reference to
SDL_PollEvent' main.o: In function pollEvents(Window&,
std::vector<Character*, std::allocator<Character*> >&, World*)':
main.cpp:(.text+0xae): undefined reference to
SDL_PollEvent' Block.o: In function Block::draw(int, int) const': Block.cpp:(.text+0xf4):
undefined reference to
SDL_SetRenderDrawColor' Block.cpp:(.text+0x10b): undefined reference to SDL_RenderFillRect'
Ladder.o: In function
Ladder::draw(int, int) const': Ladder.cpp:(.text+0x19a): undefined reference to SDL_SetRenderDrawColor' Ladder.cpp:(.text+0x1b1): undefined reference
to
SDL_RenderFillRect' Ladder.cpp:(.text+0x22e): undefined reference to SDL_SetRenderDrawColor' Ladder.cpp:(.text+0x245): undefined
reference to
SDL_RenderFillRect' Ladder.cpp:(.text+0x2dd): undefined reference to SDL_SetRenderDrawColor' Ladder.cpp:(.text+0x2f4):
undefined reference to
SDL_RenderFillRect' NPC.o: In function NPC::draw(int, int) const': NPC.cpp:(.text+0xbd0): undefined
reference to
SDL_SetRenderDrawColor' NPC.cpp:(.text+0xbe7): undefined reference to `SDL_RenderFillRect'

...

最佳答案

改变:

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) $^

关于c++ - 我在链接 SDL 库时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56460984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com