How do I keep my Qt C++ program from opening a console in Windows?

后端 未结 5 1979
有刺的猬
有刺的猬 2021-02-19 10:10

I\'m making an application in Qt Creator, with cmake and MinGW as compiler. I\'ve seen this question being answered for other people, but they used regular Qt projects with .pro

相关标签:
5条回答
  • 2021-02-19 10:59

    This is an old question, but anyways, there is a better solution than all of the other ones posted here:

    CMAKE_POLICY(SET CMP0020 NEW)
    

    Adding this will automatically handle everything for you. CMake should actually output a warning if you don't set this policy, at least that's how I learned of its existence.

    0 讨论(0)
  • 2021-02-19 11:02

    I had the same issue, but solved it by adding:

    #CMakeLists.txt
    # ... some text (like finding QT)
    
    LINK_LIBRARIES(${QT_QTMAIN_LIBRARY})
    
    # ... and then
    
    ADD_EXECUTABLE(my_qt_project WIN32 ... )
    

    If I don't use LINK_LIBRARIES(${QT_QTMAIN_LIBRARY}) I get error:

    error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

    0 讨论(0)
  • 2021-02-19 11:05

    By default, and in contrast to qmake, cmake builds Qt apps with enabled console window under windows (windows binaries can use different entry points - the console window is one of them).

    You can disable the console window appearing via setting the WIN32_EXECUTABLE cmake property on the executable.

    This can be achieved either via setting an add_executable option, i.e.

    add_executable(myexe WIN32 ...)
    

    or via setting the property explicitly:

    set_property(TARGET main PROPERTY WIN32_EXECUTABLE true)
    

    Using set_property() is helpful when the console window should conditionally be disabled, e.g.:

    if(CMAKE_BUILD_TYPE STREQUAL "Release")
      set_property(TARGET main PROPERTY WIN32_EXECUTABLE true)
    endif()
    

    The WIN32_EXECUTABLE property has no effect when compiling on platforms other than windows (cf. CMAKE_WIN32_EXECUTABLE).

    As with the WIN32 cmake variable, the WIN32_EXECUTABLE property also configures the console window when compiling a win64 executable.

    0 讨论(0)
  • 2021-02-19 11:11

    For building with Mingw, add a CMake command:

    set_target_properties(target_name PROPERTIES LINK_FLAGS "-mwindows")
    

    Replace target_name with your target's name (first parameter to add_executable)

    0 讨论(0)
  • 2021-02-19 11:12

    You will most likely have a line such as the following in your CMakeLists.txt:

    ADD_EXECUTABLE(exename ....)
    

    where of course the dots are further arguments. Change this to:

    ADD_EXECUTABLE(exename [WIN32] ...)
    

    to specify that it's a Win32 application and not a console application.

    Or, as can be found on the CMAKE website "If WIN32 is given the property WIN32_EXECUTABLE will be set on the target created." And when WIN32_EXECUTABLE is set it will "Build an executable with a WinMain entry point on windows."

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