1
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

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

4

1 回答 1

0
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 () 并修复了它。

于 2013-01-12T17:47:39.493 回答