gpt4 book ai didi

c++ - 链接无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 14:11:06 25 4
gpt4 key购买 nike

project (2dplatformer)
cmake_minimum_required (VERSION 2.8)

set(CMAKE_MODULE_PATH ${sfmle_SOURCE_DIR}/cmake/modules
${CMAKE_MODULE_PATH})

find_package(SFML 2.0 REQUIRED audio graphics network system window)

include_directories (${SFML_INCLUDE_DIR})
link_libraries (${SFML_LIBRARIES})

add_library (Tilemap.o src/Tilemap.cpp)
add_library (State.o src/State.cpp)
add_library (TitleState.o src/TitleState.cpp)
add_library (PlayState.o src/PlayState.cpp)
add_library (Player.o src/Player.cpp)

add_executable (2dplatformer src/Game.cpp)
target_link_libraries (2dplatformer sfml-audio sfml-system sfml-graphics sfml-window Player.o PlayState.o Tilemap.o State.o TitleState.o)

这是我的 CMakeLists.txt,我用它来构建我的项目。

[ 16%] Built target TitleState.o
[ 33%] Built target Tilemap.o
[ 50%] Built target Player.o
[ 66%] Built target PlayState.o
[ 83%] Built target State.o
Linking CXX executable 2dplatformer
libPlayState.o.a(PlayState.cpp.o): In function `PlayState::PlayState(Game*)':
PlayState.cpp:(.text+0x286): undefined reference to `Player::Player(Game*, Tilemap*)'
libPlayState.o.a(PlayState.cpp.o): In function `PlayState::update()':
PlayState.cpp:(.text+0x378): undefined reference to `Player::update()'
libPlayState.o.a(PlayState.cpp.o): In function `PlayState::draw()':
PlayState.cpp:(.text+0x44f): undefined reference to `Player::draw()'
collect2: error: ld returned 1 exit status
make[2]: *** [2dplatformer] Error 1
make[1]: *** [CMakeFiles/2dplatformer.dir/all] Error 2
make: *** [all] Error 2

当我使用这个写得非常糟糕的 Makefile 时,我的游戏运行正常:

LIBS=-lsfml-graphics -lsfml-window -lsfml-system

all:
@echo "*** Building the game"

g++ -c "src/Tilemap.cpp" -o "build/Tilemap.o"
g++ -c "src/State.cpp" -o "build/State.o"
g++ -c "src/TitleState.cpp" -o "build/TitleState.o"
g++ -c "src/PlayState.cpp" -o "build/PlayState.o"
g++ -c "src/Game.cpp" -o "build/Game.o"
g++ -c "src/Player.cpp" -o "build/Player.o"
g++ -o 2dplatformer build/Tilemap.o build/Game.o build/State.o build/Player.o build/TitleState.o build/PlayState.o $(LIBS)

map_editor:
@echo "*** Building the level editor"

g++ -c "src/Tilemap.cpp" -o "build/Tilemap.o"
g++ -c "src/Editor.cpp" -o "build/Editor.o"

g++ -o editor build/Tilemap.o build/Editor.o $(LIBS)

clean:
@echo "*** Removing object files and executable..."

rm -rf build/*
rm -f 2dplatformer

我正在链接库,所以我不知道为什么找不到它们。

最佳答案

project (2dplatformer)
cmake_minimum_required (VERSION 2.8)

set(CMAKE_MODULE_PATH ${sfmle_SOURCE_DIR}/cmake/modules
${CMAKE_MODULE_PATH})

find_package(SFML 2.0 REQUIRED audio graphics network system window)

include_directories (${SFML_INCLUDE_DIR})
link_libraries (${SFML_LIBRARIES})

add_library (Tilemap.o src/Tilemap.cpp)
add_library (State.o src/State.cpp)
add_library (TitleState.o src/TitleState.cpp)
add_library (PlayState.o src/PlayState.cpp)
add_library (Player.o src/Player.cpp)

link_libraries (Player.o PlayState.o Tilemap.o State.o TitleState.o)

add_executable (2dplatformer src/Game.cpp)
target_link_libraries (2dplatformer Player.o PlayState.o Tilemap.o State.o TitleState.o)

我添加了一个 link_libraries () 并修复了它。

关于c++ - 链接无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14295863/

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