Search code examples
cmakeqmlqmakeqt6

What is equivalent of qmake (.pro file) CONFIG -= console in CMAKE


I have build QML application and run it. But besides of GUI running console. I know how switch off console in qmake (.pro file) CONFIG -= console. But I do not understand how it works in CMake? I tried QML opens GUI window and console but it does not helped me.

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

project(APSMDClient LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 14)
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 Core Quick Qml LinguistTools QuickControls2 REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick LinguistTools QuickControls2 REQUIRED)


set(TS_FILES APSMDClient_ru_RU.ts)

set(PROJECT_SOURCES
        main.cpp
        qml.qrc
        ${TS_FILES}
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(APSMDClient
        ${PROJECT_SOURCES}
    )

    qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
else()
    if(ANDROID)
        add_library(APSMDClient SHARED
            ${PROJECT_SOURCES}
        )
    else()
        add_executable(APSMDClient
          ${PROJECT_SOURCES}
        )
    endif()

    qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
endif()

# Added
add_executable(APSMDClient WIN32 ${SOURCE_FILES})

target_link_libraries(APSMDClient PRIVATE Qt6::QuickControls2 Qt6::Qml)

target_compile_definitions(APSMDClient
  PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(APSMDClient
  PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick)

Solution

  • To disable the console window you can use either

    add_executable(APSMDClient WIN32 ${PROJECT_SOURCES})
    

    or

    add_executable(APSMDClient ${PROJECT_SOURCES})
    if (WIN32)
        set_target_properties(APSMDClient PROPERTIES WIN32_EXECUTABLE TRUE)
    endif()
    

    qt_add_executable is a Qt wrapper for add_executable.