Using SDL2 with CMake

前端 未结 12 856
野性不改
野性不改 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:58

    Highlighting the steps of how I was able to eventually accomplish this using the FindSDL2.cmake module:

    • Download SDL2-devel-2.0.9-VC.zip (or whatever version is out after this answer is posted) under the Development Libraries section of the downloads page.
    • Extract the zip folder and you should see a folder similar to "SDL2-2.0.9". Paste this folder in your C:\Program Files(x86)\ directory.
    • Copy the FindSDL2.cmake module and place it in a new "cmake" directory within your project. I found a FindSDL2.cmake file in the answer referenced in the Accepted Answer: https://brendanwhitfield.wordpress.com/2015/02/26/using-cmake-with-sdl2/
    • Find the SET(SDL2_SEARCH_PATHS line in the FindSDL2.cmake and add your copied development directory for SDL2 as a new line: "/Program Files (x86)/SDL2-2.0.9" # Windows
    • Within my CMakeLists.txt, add this line: set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

    After this, running CMake worked for me. I'm including the rest of my CMakeLists just in case it further clarifies anything I may have left out:

    cmake_minimum_required(VERSION 2.8.4)
    project(Test_Project)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    
    # includes cmake/FindSDL2.cmake
    set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
    
    set(SOURCE_FILES src/main.cpp src/test.cpp)
    add_executable(test ${SOURCE_FILES})
    
    # The two lines below have been removed to run on my Windows machine
    #INCLUDE(FindPkgConfig)
    #PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
    find_package(SDL2 REQUIRED)
    
    INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIR})
    TARGET_LINK_LIBRARIES(chip8 ${SDL2_LIBRARY})
    

    Hope this helps somebody in the near future.

提交回复
热议问题