链接无法正常工作

时间:2013-01-12 17:23:47

标签: c++ cmake sfml

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

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

1 个答案:

答案 0 :(得分: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()并修复了它。