Linking SDL2 and Clion

前端 未结 1 756
梦谈多话
梦谈多话 2021-01-20 10:46

I have clion pointing to SDL2 directories and libs, but it fails to link the libraries when I try to build. Any ideas on how to fix this?

CMakeLists:

相关标签:
1条回答
  • 2021-01-20 11:30

    Here is a minimal example to get you started with SDL2 on windows (main.cpp just contains the hello SDL from LazyFoo)

    cmake_minimum_required(VERSION 3.0)
    project(hello_sdl2)
    
    # configure the SDL (cf. "SDL2-2.0.3\i686-w64-mingw32\lib\pkgconfig\sdl2.pc")
    # C++ flags
    set(SDL2_Flags "-mwindows -Wl,--no-undefined -static-libgcc")
    # library paths
    set(SDL2_ROOT     "D:/PATH/TO/SDL2-2.0.3/i686-w64-mingw32")
    set(SDL2_Includes "${SDL2_ROOT}/include")
    set(SDL2_LibDir   "${SDL2_ROOT}/lib")
    # imported targets for CMake (cf. https://cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets)
    add_library(SDL2     STATIC IMPORTED)
    add_library(SDL2main STATIC IMPORTED)
    set_property(TARGET SDL2     PROPERTY IMPORTED_LOCATION "${SDL2_LibDir}/libSDL2.a")
    set_property(TARGET SDL2main PROPERTY IMPORTED_LOCATION "${SDL2_LibDir}/libSDL2main.a")
    # the libs to link against
    # note: as always with gcc, the order is important...
    set(SDL2_Libs mingw32 SDL2 SDL2main m dinput8 dxguid dxerr8 user32 gdi32 winmm imm32 ole32 oleaut32 shell32 version uuid)
    
    # configure the project
    # include the SDL flags
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${SDL2_Flags}")
    # collect the sources
    set(SOURCE_FILES main.cpp)
    # define the target
    add_executable(hello_sdl2 ${SOURCE_FILES})
    # include the SDL headers
    target_include_directories(hello_sdl2 SYSTEM PRIVATE ${SDL2_Includes})
    # link against the SDL (and its dependencies)
    target_link_libraries(hello_sdl2 ${SDL2_Libs})
    

    tested on Win8.1 64bits with SDL2-2.0.3, MinGW-W64 i686-5.2.0-posix-dwarf-rt_v4-rev0 and CLion 1.2 EAP (build 142.5239.6)

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