如何防止我的 Qt C++ 程序在 Windows 中打开控制台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8249028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:31:03  来源:igfitidea点击:

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

c++windowsqtconsolecmake

提问by Neko

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 files, while I use a CMakeLists.txt file. So these posts were of no help to me.

我正在 Qt Creator 中制作一个应用程序,使用 cmake 和 MinGW 作为编译器。我已经看到其他人回答了这个问题,但他们使用带有 .pro 文件的常规 Qt 项目,而我使用的是 CMakeLists.txt 文件。所以这些帖子对我没有帮助。

The problem is that my application opens a console when booted, and as usual, closing this console will close the application as well. I want to keep the application from opening a console, so that it is more user-friendly for people who don't need any debug information and such.

问题是我的应用程序在启动时会打开一个控制台,像往常一样,关闭这个控制台也会关闭应用程序。我想阻止应用程序打开控制台,以便对于不需要任何调试信息等的人来说更加用户友好。

采纳答案by Bart

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

您的 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.

指定它是 Win32 应用程序而不是控制台应用程序。

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

或者,可以在 CMAKE 网站上找到“如果WIN32给出该属性,WIN32_EXECUTABLE将在创建的目标上设置。” 当设置 WIN32_EXECUTABLE 时,它将“在 Windows 上使用 WinMain 入口点构建可执行文件”。

回答by maxschlepzig

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).

默认情况下,与 qmake 相比,cmake 在 windows 下构建启用控制台窗口的 Qt 应用程序(windows 二进制文件可以使用不同的入口点 - 控制台窗口就是其中之一)。

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

您可以通过WIN32_EXECUTABLE在可执行文件上设置cmake 属性来禁用出现的控制台窗口。

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

这可以通过设置一个add_executable选项来实现,即

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.:

set_property()当控制台窗口应有条件地禁用时,使用会很有帮助,例如:

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

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

WIN32_EXECUTABLEWindows 以外的平台上编译时,该属性无效(参见CMAKE_WIN32_EXECUTABLE)。

As with the WIN32cmake variable, the WIN32_EXECUTABLEproperty also configures the console window when compiling a win64 executable.

WIN32cmake 变量一样,该WIN32_EXECUTABLE属性还在编译 win64 可执行文件时配置控制台窗口。

回答by Jun Murakami

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:

如果我不使用LINK_LIBRARIES(${QT_QTMAIN_LIBRARY})我得到错误:

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

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

回答by Silas Parker

For building with Mingw, add a CMake command:

要使用 Mingw 进行构建,请添加 CMake 命令:

set_target_properties(target_name PROPERTIES LINK_FLAGS "-mwindows")

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

替换target_name为目标名称(add_executable 的第一个参数)

回答by clocktown

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.

添加它会自动为你处理一切。如果你没有设置这个策略,CMake 实际上应该输出一个警告,至少我是这样知道它的存在的。