Using SDL2 with CMake

前端 未结 12 883
野性不改
野性不改 2020-11-28 10:47

I\'m trying to use CLion to create a SDL2 project. The problem is that the SDL headers can\'t be found when using #include\'s.

My CMakeLists.txt file:



        
12条回答
  •  伪装坚强ぢ
    2020-11-28 10:54

    This blog post shows how you can do it: Using SDL2 with CMake

    On Linux you can use a recent CMake (e.g. version 3.7) and using SDL2 works out of the box.

    cmake_minimum_required(VERSION 3.7)
    project(SDL2Test)
    
    find_package(SDL2 REQUIRED)
    include_directories(SDL2Test ${SDL2_INCLUDE_DIRS})
    
    add_executable(SDL2Test Main.cpp)
    target_link_libraries(SDL2Test ${SDL2_LIBRARIES})
    

    Under Windows you can download the SDL2 development package, extract it somewhere and then create a sdl-config.cmake file in the extracted location with the following content:

    set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include")
    
    # Support both 32 and 64 bit builds
    if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
      set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2main.lib")
    else ()
      set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2main.lib")
    endif ()
    
    string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
    

    When you now configure inside the CMake-GUI application there will be a SDL2_DIR variable. You have to point it to the SDL2 directory where you extracted the dev package and reconfigure then everything should work.

    You can then include SDL2 headers by just writing #include "SDL.h".

提交回复
热议问题