为什么我的 Qt 4.5 应用程序会在 Windows 下打开一个控制台窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/760323/
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-09 06:15:47  来源:igfitidea点击:

Why does my Qt 4.5 app open a console window under Windows?

windowsqtconsolemingwqt-creator

提问by Paul Dixon

I've been playing around with Qt Creator 4.5 under Linux. My application builds just fine under Linux, but if I build in Windows, the app always opens a console window at startup.

我一直在 Linux 下玩 Qt Creator 4.5。我的应用程序在 Linux 下构建得很好,但如果我在 Windows 中构建,应用程序总是在启动时打开一个控制台窗口。

Can I stop it doing that?

我可以阻止它这样做吗?

I'm building using the default MinGW setup, perhaps that is related. If need be I can build with Visual Studio, but I'd like to understand what is happening first...

我正在使用默认的 MinGW 设置进行构建,这可能是相关的。如果需要,我可以使用 Visual Studio 构建,但我想先了解发生了什么......

Edit: I just created a simple test GUI app with Qt Creator under Windows and it didn't exhibit this behaviour. Either this behaviour has occurred because of the way the project was created under linux, or there is something my app does which causes the console window to appear. Will post details when I diagnose it in case it helps someone else.

编辑:我刚刚在 Windows 下使用 Qt Creator 创建了一个简单的测试 GUI 应用程序,但它没有表现出这种行为。这种行为要么是由于项目在 linux 下创建的方式造成的,要么是我的应用程序执行的某些操作导致控制台窗口出现。当我诊断它时会发布详细信息,以防它对其他人有帮助。

采纳答案by Paul Dixon

The short answer is that including the Qt testlib causes the console to appear. Removing it makes it go away.

简短的回答是,包含 Qt 测试库会导致控制台出现。删除它会使它消失。

To explain further, if your .pro file adds testlib to QT, e.g.

为了进一步解释,如果您的 .pro 文件将 testlib 添加到 QT,例如

QT += sql \
    webkit \
    network \
    testlib

then the final link step is carried out with a line like this

那么最后的链接步骤是用这样的一行来执行的

g++ -enable-stdcall-fixup 
   -Wl,-enable-auto-import 
   -Wl,-enable-runtime-pseudo-reloc 
   -mthreads 
   -Wl 
   -Wl,-subsystem,console 
   -o debug\MyApp.exe object_script.MyApp.Debug  
   -L"c:\Qt09.01\qt\lib"  
   -lglu32 -lgdi32 -luser32 -lQtWebKitd4 -lQtTestd4 
   -lQtSqld4 -lQtGuid4 -lQtNetworkd4 -lQtCored

We've wound up using the console subsystem! I presume using testlib forces this to happen so that the test output has somewhere to go.

我们已经结束使用控制台子系统!我认为使用 testlib 会强制发生这种情况,以便测试输出可以去某个地方。

If we now edit the .pro file and remove the reference to testlib and rebuild, we get a link step like this

如果我们现在编辑 .pro 文件并删除对 testlib 和重建的引用,我们会得到这样的链接步骤

g++ -enable-stdcall-fixup 
   -Wl,-enable-auto-import 
   -Wl,-enable-runtime-pseudo-reloc 
   -mthreads 
   -Wl 
   -Wl,-subsystem,windows 
   -o debug\Ammotin.exe object_script.Ammotin.Debug  
   -L"c:\Qt09.01\qt\lib" 
   -lglu32 -lgdi32 -luser32 -lmingw32 -lqtmaind -lQtWebKitd4 
   -lQtSqld4  -lQtGuid4 -lQtNetworkd4 -lQtCored4

Yay! subsystem is windows, no more console window.

好极了!子系统是窗口,没有更多的控制台窗口。

回答by Lion

For those who have this issue using CMake, I've found the real solution here. Verified by myself.

对于那些使用 CMake 遇到此问题的人,我在这里找到了真正的解决方案。本人验证。

In short, you need to add WIN32to your add_executable()statements:

简而言之,您需要WIN32add_executable()语句中添加:

add_executable(GuiApplication WIN32 src/main.cpp)

For further details, see the CMake documentation on add_executableand WIN32_EXECUTABLE.

有关详细信息,请参阅CMake的文件add_executableWIN32_EXECUTABLE

回答by fbeeper

I think that this is not a solution for this specific answer (besides it is 4 years later), but I think that many people landing in this thread will be looking for this setting:

我认为这不是这个特定答案的解决方案(除了它是 4 年后),但我认为很多登陆这个线程的人都会寻找这个设置:

Projects > Build and Run > Run Settings > Run > [x] Run in terminal

项目 > 构建和运行 > 运行设置 > 运行 > [x] 在终端中运行

De-select it and run your GUI from QtCreator without an extra Terminal window. Terminal output will be then embedded in the IDE.

取消选择它并从 QtCreator 运行您的 GUI,而无需额外的终端窗口。然后终端输出将嵌入到 IDE 中。

回答by John T

You will want to make sure the -mwindows switch is provided.

您需要确保提供了 -mwindows 开关。

Edit:

编辑:

alternatively, you can go into your makefile and add this to your linker flags:

或者,您可以进入您的 makefile 并将其添加到您的链接器标志中:

-Wl,-subsystem,windows

回答by bluebrother

Make sure your .pro file doesn't add consoleto the variable CONFIG. You can do this by adding

确保您的 .pro 文件不会添加console到变量中CONFIG。您可以通过添加来做到这一点

CONFIG -= console

somewhere at the end of your .pro file. If CONFIGcontains consolea window pops up every time you start the program, and this is also used when printing debug output. Thus, adding consolecan be useful when debugging a program. Otherwise you'd need to use tools like DebugViewto show the output of the qDebug()calls.

在 .pro 文件末尾的某个位置。如果CONFIG包含console一个窗口,每次启动程序时弹出,并打印调试输出时,这也被使用。因此,添加console在调试程序时很有用。否则,您需要使用DebugView 之类的工具来显示qDebug()调用的输出。

回答by Craig

I use cmake instead of qmake. I used set(CMAKE_CXX_FLAGS "-mwindows") seems as though QMAKE_CXXFLAGS +=-mwindows in your .pro file would do the same.

我使用 cmake 而不是 qmake。我使用 set(CMAKE_CXX_FLAGS "-mwindows") 好像 QMAKE_CXXFLAGS +=-mwindows 在你的 .pro 文件中会做同样的事情。

回答by zooropa

Sounds like your linker settings are set for a console app. You could try setting the linker to use the /subsystem:windows option. The option for console applications is /subsystem:console. If you build your project in Visual Studio, create a Win32 Project. Don't create a Win32 Console App. The option under Visual Studio for setting the subsystem is under Project Settings->Linker->System->SubSystem. Select Windows(/subsystem:windows).

听起来您的链接器设置是为控制台应用程序设置的。您可以尝试将链接器设置为使用 /subsystem:windows 选项。控制台应用程序的选项是 /subsystem:console。如果在 Visual Studio 中构建项目,请创建一个 Win32 项目。不要创建 Win32 控制台应用程序。Visual Studio下设置子系统的选项在Project Settings->Linker->System->SubSystem下。选择 Windows(/subsystem:windows)。

I took a look into how to do this with MinGW/gcc. The online docs for gccshows the different options for Windows targets. Like other have stated, the option you are looking for is -mwindows. The option -mwindows creates a windowed app. It seems like your current setting is -mconsole. The option -mconsole produces a console app. This is a linker option. I'm not familiar with Qt Creator but I'm guessing it has a similar way to view project settings like Visual Studio has.

我研究了如何使用 MinGW/gcc 来做到这一点。gcc的在线文档显示了 Windows 目标的不同选项。像其他人所说的那样,您正在寻找的选项是 -mwindows。选项 -mwindows 创建一个窗口应用程序。您当前的设置似乎是 -mconsole。选项 -mconsole 生成一个控制台应用程序。这是一个链接器选项。我不熟悉 Qt Creator,但我猜它有一种类似于 Visual Studio 的查看项目设置的方式。

回答by Alex Ruse

By default the linker assumes you want a console application.

默认情况下,链接器假定您需要控制台应用程序。

The fix is to add "-mwindows" to your compiler's argument list, and this will also invisibly add the required libraries (-lkernel32, -lgdi32, etc.). You'll probably need to change from main() to WinMain() as the entry point.

修复方法是将“-mwindows”添加到编译器的参数列表中,这也会无形地添加所需的库(-lkernel32、-lgdi32 等)。您可能需要将 main() 更改为 WinMain() 作为入口点。

RE: your Edit:the Qt build system uses "spec" files to add variables, you can find them in $QTDIR/qt/mkspecs. When you create a new Windows app in QCreator, it sets the default spec to "win32-g++", which automatically sets the correct variables (windows app, linker flags, etc.).

RE:您的编辑:Qt 构建系统使用“spec”文件来添加变量,您可以在 $QTDIR/qt/mkspecs 中找到它们。当您在 QCreator 中创建新的 Windows 应用程序时,它会将默认规范设置为“win32-g++”,这会自动设置正确的变量(Windows 应用程序、链接器标志等)。

When you made the project under Linux, it used another spec and that caused your issue. You can see the current spec in the "yourproject.pro.user" file.

当您在 Linux 下创建项目时,它使用了另一个规范,这导致了您的问题。您可以在“yourproject.pro.user”文件中查看当前规范。

回答by Artem Zaytsev

Go to: Projects --> Runand uncheck Run in terminalcheckbox

转到:Projects --> Run并取消选中Run in terminal复选框

回答by Eda

Qt/2009.05/qt/mkspecs/win32-g++
edit file qmake.conf
modify line
QMAKE_LFLAGS_CONSOLE = -Wl,-subsyetem,console
to
QMAKE_LFLAGS_CONSOLE = -Wl,-subsyetem,windows

Qt/2009.05/qt/mkspecs/win32-g++
编辑文件 qmake.conf
修改行
QMAKE_LFLAGS_CONSOLE = -Wl,-subsyetem,console

QMAKE_LFLAGS_CONSOLE = -Wl,-subsyetem,windows

and modify your ???.pro file (example add new line or space)
and compiling
it`s working ok

并修改您的 ???.pro 文件(例如添加新行或空格)
并编译它
工作正常