Using SDL2 with CMake

前端 未结 12 833
野性不改
野性不改 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:50
    cmake_minimum_required(VERSION 2.8.4)
    
    project(ChickenShooter)
    
    set(SDL2_INCLUDE_DIR C:/SDL/SDL2-2.0.3/include/SDL2)
    set(SDL2_LIB_DIR C:/SDL/SDL2-2.0.3/lib/x64)
    
    include_directories(${SDL2_INCLUDE_DIR})
    link_directories(${SDL2_LIB_DIR})
    
    set(SOURCE_FILES main.cpp)
    
    add_executable(${PROJECT_NAME} ${SOURCE_FILES})
    target_link_libraries(${PROJECT_NAME} SDL2main SDL2)
    
    0 讨论(0)
  • 2020-11-28 10:51

    For your information, I was able to successfully cmake and compile SDL2_ttf while linking to SDL2 source code.

    At first I was getting errors due to cmake not being able to locate SDL2, even though it was specified in cmake using the SLD2_DIR variable in cmake.

    It seems that for some reason cmaking SDL2 fails to create the SDL2Targets.cmake file which is searched for by SDL2_ttf

    If this is the case for you, get the SDL2Targets.cmake file from https://bugs.archlinux.org/task/57972 and modify the file like so:

    You can remove the following lines:

    get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
    get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
    get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
    get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
    if(_IMPORT_PREFIX STREQUAL "/")
        set(_IMPORT_PREFIX "")
    endif()
    

    and add this one:

    set(_IMPORT_PREFIX "C:/SDL2-2.0.12")
    

    Obviously change the filepath to the place you unpacked the SDL2 source code

    I'm not sure if this is exactly your issue, but there it is.

    0 讨论(0)
  • 2020-11-28 10:53

    Don't set the path to SDL2 by hand. Use the proper find command which uses FindSDL. Should look like:

    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})    
    

    If SDL2 is not found, you have to add the path to SDL2 to CMAKE_PREFIX_PATH, that's the place where CMake looks for installed software.

    If you can use Pkg-config, its use might be easier, see How to use SDL2 and SDL_image with cmake

    If you feel more comfortable to use a FindSDL2.cmake file similar to FindSDL.cmake provided by CMake, see https://brendanwhitfield.wordpress.com/2015/02/26/using-cmake-with-sdl2/

    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2020-11-28 10:54

    by the time of my answer, SDL2 is provided with sdl2-config executable (as I understand, developers call him "experimental"). After "make install" of SDL2 you can try calling it from terminal with sdl2-config --cflags --libs to see what it outputs.

    And then you can add call to it in your makefile:

    set(PROJECT_NAME SomeProject)
    
    project(${PROJECT_NAME})
    
    execute_process(COMMAND /usr/local/bin/sdl2-config --libs RESULT_VARIABLE CMD_RES OUTPUT_VARIABLE SDL2_CFLAGS_LIBS ERROR_VARIABLE ERR_VAR OUTPUT_STRIP_TRAILING_WHITESPACE)
    message("SDL2_CFLAGS_LIBS=${SDL2_CFLAGS_LIBS}; CMD_RES=${CMD_RES}; ERR_VAR=${ERR_VAR}")
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${SDL2_CFLAGS_LIBS}")
    
    set(SOURCE_FILES main.cpp)
    add_executable(${PROJECT_NAME} ${SOURCE_FILES})
    

    Here I have a problem - if I only put an executable name without path like

    execute_process(COMMAND sdl2-config --libs <...>
    

    I get error "No such file", i.e. cmake does not search in current path and I don't know how to write it properly by now.

    One more notice: in my makefile I do not user --cflags option, because cmake finds includes correctly and I do not need to specify them explicitly.

    0 讨论(0)
  • 2020-11-28 10:58

    You can also pull in the SDL source repository as a submodule and build/link it statically along with your main program via add_subdirectory() and target_link_libraries():

    cmake_minimum_required( VERSION 3.7.0 )
    project( sdl2-demo )
    
    set( SDL_STATIC ON CACHE BOOL "" FORCE )
    set( SDL_SHARED OFF CACHE BOOL "" FORCE )
    add_subdirectory( external/sdl )
    
    add_executable(
        sdl2-demo
        "src/main.cpp"
        )
    target_link_libraries( sdl2-demo SDL2main SDL2-static )
    

    (At least as of the release-2.0.9 tag, possibly earlier.)

    0 讨论(0)
提交回复
热议问题