Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to change CMake version and Include SDL2?



  • Hi there.. again..
    I´ve some Problems with the QT Creator and how to change cmake Version.

    I´ve installed a newer version of cmake 3.20.1 on my Windows PC and want my QT cmake Project to use that version, instead of the QT internal cmake.
    But in the Build&Run Option of Cmake is no add Button, as shown in the Doc .. instead, there is only a Radio Option
    A.jpg

    How can i change the cmake Version?

    As second.. I created a cmake Project, not QMake.. So i´ve no *.pro File here.. just the CMakeList.txt.

    I´ve downloaded the Dev Libs of SDL2 and put the needed Files into the QT Mingw 64Bit Version Subfolders (as stated in the SDL2 install Guide).
    Now the compiler throws out an Error, that he can´t find SDL2..
    A2.jpg

    So..

    find_package(SDL2 REQUIRED)
    

    Is not working in the CmakeList...
    This is my current CMakeList.txt

    cmake_minimum_required(VERSION 3.5)
    
    project(TGS_Editor LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    # QtCreator supports the following variables for Android, which are identical to qmake Android variables.
    # Check https://doc.qt.io/qt/deployment-android.html for more information.
    # They need to be set before the find_package( ...) calls below.
    
    #if(ANDROID)
    #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    #    if (ANDROID_ABI STREQUAL "armeabi-v7a")
    #        set(ANDROID_EXTRA_LIBS
    #            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
    #            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
    #    endif()
    #endif()
    
    find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
    find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
    find_package(SDL2 REQUIRED)
    
    include_directories(${SDL_INCLUDE_DIR})
    
    set(PROJECT_SOURCES
            main.cpp
            tgs_window_main.cpp
            tgs_window_main.h
            tgs_window_main.ui
    )
    
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        qt_add_executable(TGS_Editor
            ${PROJECT_SOURCES}
        )
    else()
        if(ANDROID)
            add_library(TGS_Editor SHARED
                ${PROJECT_SOURCES}
            )
        else()
            add_executable(TGS_Editor
                ${PROJECT_SOURCES}
            )
        endif()
    endif()
    
    
    target_link_libraries(TGS_Editor PRIVATE Qt${QT_VERSION_MAJOR}::Widgets ${SDL2_LIBRARIES})
    
    

    Any hints on how to include SDL2 into my project

    Project is freshly created with cmake and Mingw for 64Bit as Kit and Build&Run..
    (QT Online Installer)
    Planned to inlcude is: OpenGl ES, Angle, SDL2


  • Lifetime Qt Champion

    You have to select the cmake version you want to use in the Kit settings.


  • Lifetime Qt Champion

    You have to select the cmake version you want to use in the Kit settings.



  • @Christian-Ehrlicher Thank you.. now is just the SDL2 Part missing..


  • Lifetime Qt Champion

    Either SDL2 ships a proper cmake file or you have to add the include dir and library by yourself (target_include_directories() and target_link_libraries())



  • @Christian-Ehrlicher Yes.. tried target_link_libraries..
    there is not much info on how to use sdl2, with cmake under QT under Windows.. so.. how to do that, cause my approach isn't working..


  • Lifetime Qt Champion

    @BDC_Patrick said in How to change CMake version and Include SDL2?:

    so.. how to do that,

    You should ask the SDL2 guys which library you have to link to. And isn't working is not error description at all...



  • @Christian-Ehrlicher That´s why i posted a Screenshot of the occuring Error in the OP ^^



  • I found a solution by myself with trial&Error.

    Here is a complete CmakeList.txt for you:

    cmake_minimum_required(VERSION 3.5)
    
    project(MyProgram LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    # QtCreator supports the following variables for Android, which are identical to qmake Android variables.
    # Check https://doc.qt.io/qt/deployment-android.html for more information.
    # They need to be set before the find_package( ...) calls below.
    
    #if(ANDROID)
    #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    #    if (ANDROID_ABI STREQUAL "armeabi-v7a")
    #        set(ANDROID_EXTRA_LIBS
    #            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
    #            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
    #    endif()
    #endif()
    
    find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
    find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
    find_package(SDL2)
    
    include_directories(${SDL2_INCLUDE_DIR})
    
    set(PROJECT_SOURCES
            main.cpp
            mainWin.cpp
            mainWin.h
            mainWin.ui
    )
    
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        qt_add_executable(MyProgram 
            ${PROJECT_SOURCES}
        )
    else()
        if(ANDROID)
            add_library(MyProgram SHARED
                ${PROJECT_SOURCES}
            )
        else()
            add_executable(MyProgram 
                ${PROJECT_SOURCES}
            )
        endif()
    endif()
    
    target_link_libraries(MyProgram PRIVATE Qt${QT_VERSION_MAJOR}::Widgets ${SDL2_LIBRARIES})
    
    

    include via

    #include <SDL2\SDL.h>
    

    And don´t forget to change the

    int main(int argc, char *argv[])
    

    in your main.cpp to:

    int WinMain(int argc, char *argv[])
    

    Works now without Errors..


  • Lifetime Qt Champion

    @BDC_Patrick said in How to change CMake version and Include SDL2?:

    int WinMain(int argc, char *argv[])

    Why? Is this needed by SDL?



  • @Christian-Ehrlicher I changed it back to main..

    SDL complains missing references to WinMain.. undefining it, before including SDL, works.


Log in to reply