2017-10-10 157 views
0

我想在SDL2中使用Clion,經過大量測試後,我已經有一個錯誤。Clion Cmake&SDL2

有我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.8) 
project(sdl2-test) 

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32") 
#set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") 
set(CMAKE_CXX_STANDARD 11) 
include_directories(${PROJECT_SOURCE_DIR}/include) 
link_directories(${PROJECT_SOURCE_DIR}/lib) 

set(SOURCE_FILES main.cpp) 
add_executable(sdl2-test ${SOURCE_FILES}) 

target_link_libraries(sdl2-test SDL2main SDL2) 

我的main.cpp

#include <iostream> 

#include "include/SDL2/SDL.h" 

using namespace std; 

int main(int argc, char** argv) { 
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1){ 
     cout << "Something went wrong! " << SDL_GetError() << endl; 
    } 

    SDL_Window* window = SDL_CreateWindow("SDL_Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
             1280, 720, SDL_WINDOW_OPENGL); 
    if(window == nullptr){ 
     cout << "Something also went wrong here" << endl; 
    } 

    SDL_Delay(2000); 
    SDL_Quit(); 
    return 0; 

} 

但是當我建立我的cmake項目的cmake顯然找到工作,但我haave一些錯誤:

C:\Users\paulp\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\172.4343.16 \bin\cmake\bin\cmake.exe --build C:\Users\paulp\CLionProjects\sdl2-test\cmake- build-debug --target sdl2-test -- -j 4 
[ 50%] Building CXX object CMakeFiles/sdl2-test.dir/main.cpp.obj 
[100%] Linking CXX executable sdl2-test.exe 
CMakeFiles\sdl2-test.dir/objects.a(main.cpp.obj): In function `SDL_main': 
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:8: undefined reference to `SDL_Init' 
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:9: undefined reference to `SDL_GetError' 
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:13: undefined reference to `SDL_CreateWindow' 
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:18: undefined reference to `SDL_Delay' 
C:/Users/paulp/CLionProjects/sdl2-test/main.cpp:19: undefined reference to `SDL_Quit' 
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `[email protected]' 
collect2.exe: error: ld returned 1 exit status 
CMakeFiles\sdl2-test.dir\build.make:96: recipe for target 'sdl2-test.exe' failed 
mingw32-make.exe[3]: *** [sdl2-test.exe] Error 1 
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/sdl2-test.dir/all' failed 
mingw32-make.exe[2]: *** [CMakeFiles/sdl2-test.dir/all] Error 2 
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/sdl2-test.dir/rule' failed 
mingw32-make.exe[1]: *** [CMakeFiles/sdl2-test.dir/rule] Error 2 
mingw32-make.exe: *** [sdl2-test] Error 2 
Makefile:117: recipe for target 'sdl2-test' failed 

任何想法如何解決?

+0

我猜你下載了正確的mingw devel版本 – user1754322

+0

我想,我下載SDL2-devel-2.0.6-mingw.tar.gz並使用mingw 5.0 –

回答

0

您的應用程序鏈接失敗,因爲鏈接器不知道sdl2-lib的位置。

你需要搜索SDL2-庫,以及在你的cmake文件和位置添加到您的Makefile:

find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2) 
find_library(SDL2_LIBRARY NAME SDL2) 
add_executable(ChickenShooter main.cpp) 
target_include_directories(ChickenShooter ${SDL2_INCLUDE_DIR}) 
target_link_libraries(ChickenShooter ${SDL2_LIBRARY}) 

你可以看看這個帖子還有:How to dins SDL2 via cmake

+0

CMakeLake.txt:18(target_include_directories)錯誤: target_include_directories用無效參數調用 –

+0

如何在我的生成文件上添加位置? –

+0

在我的示例中由target_include_directories完成(ChickenShooter $ {SDL2_INCLUDE_DIR}) target_link_libraries(ChickenShooter $ {SDL2_LIBRARY}) – KimKulling