68

I have a Qt application, and when I run this application, there is a console opening behind it. In development it is nice because i see debug outputs on the console, but when I want to give this executable to the customer there should be no console window. how do I hide it?

(I am using Visual Studio 2008)

HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
ufukgun
  • 6,889
  • 8
  • 33
  • 55

13 Answers13

158

In the project build linker options set

/SUBSYSTEM:windows
/ENTRY:mainCRTStartup

Or use the following #pragma in the source file with the int main(...)

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • 11
    Imo the best and to-the-point answer, dealing with both the subsystem part and the different entry points part. Small unicode hint: if you compile with unicode and have wmain as opposed to main, comment should be "/ENTRY:wmainCRTStartup". – Cray Dec 03 '11 at 02:20
  • @Cray: WCHAR is not Unicode. If you're using real Unicode with the appropriate libraries like ICU, normal mainCRTStrartup works just fine. – datenwolf May 09 '12 at 11:30
  • Sure, however I was talking about the compiler option (ie, "compile with unicode"). They are actually calling it "Use Unicode Character Set". – Cray May 12 '12 at 10:28
  • This solution worked for me and my OpenGL application. I started the project months ago as a console application, using the console window for early debugging. Starting the project again as a windows application and pasting all of the files over, as well as re-entering all of the library dependencies just seemed tedious. – Guy Joel McLean Apr 26 '13 at 09:48
  • 2
    @GuyJoelMcLean: Pro-Tipp: If you need a console later on for debugging you can use AllocConsole http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944(v=vs.85).aspx – datenwolf Apr 26 '13 at 14:42
  • 2
    In VS 2010 the pragma didn't work for me. I had to set the two settings through project properties, removed _CONSOLE from the preprocessor. Then I rebuilt, and it worked perfectly. – John Neuhaus Aug 11 '15 at 20:32
  • 1
    This should be the accepted answer. E.g. if you are developing with CMake on Windows, this is the only option. Also, it is portable to other windowing systems, such as GLFW, SDL, ... – alexpanter Oct 06 '19 at 11:49
  • 1
    This helped out very much! – Noah.Ehrnstrom Sep 21 '20 at 13:08
  • Worked for me. Qt6, Win10, MSVC2019, amd64 build. – Radim Cernej Oct 26 '21 at 22:20
71

It sounds like your linker configuration is incorrect. Right-click the project, Properties, Linker, System, SubSystem setting. Make sure "Windows" is selected, not "Console".

And, change main() to WinMain().

user366312
  • 16,949
  • 65
  • 235
  • 452
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    is there a difference between main() and WinMain()? – ufukgun Jan 27 '10 at 07:39
  • 8
    You may also set the entry point of the project to standard `main()` in Properties -> Linker -> Advanced -> Entry point – Jan Turoň Mar 11 '12 at 14:56
  • 2
    The problem is that when I turn the program in `Windows` it doesn't terminate, even if I fix the `main`/`WinMain` issue. – Tomáš Zato Dec 08 '14 at 15:30
  • @JanTuroň How? What option do we put in that field? It's a string field and I've tried a wide variety of solutions, none of which work. – Nic May 01 '18 at 19:05
  • @NicHartley back then in 2008 it should be a selectbox. Perharps there are other settings in config. Could you ask another question and provide step by step setup to reproduce that? – Jan Turoň May 02 '18 at 11:41
24

You can get rid of the console by calling:

FreeConsole();
Daniel Munoz
  • 1,865
  • 1
  • 14
  • 9
14

i use that method and it worked

HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, 0);
ufukgun
  • 6,889
  • 8
  • 33
  • 55
  • 6
    If you start the program from command line the command line will disappear. `FreeConsole` does not have that issue. – nwp Dec 18 '14 at 16:35
9

Next solution ;)

Env: WixXP x64, msvs 2008, Qt v4.5.3

  1. Set Projects settings/Configuration properties/Linker/System/SubSystem = Windows (/SUBSYSTEM:WINDOWS)

    But For x64 there is linker error: LNK2019: unresolved external symbol _WinMain referenced in function _WinMainCRTStartup" To avoid it

  2. Replace the following code:

    int main(int argc, char *argv[])
    {
         QApplication app(argc, argv);
         // your code*
    }
    

    by

    int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
    {
        int argc = 0;
        QApplication app( argc, 0 );
     }
    

It works fine for both - Win32 and x64 platforms.

Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
  • 1
    This method will prevent the program from getting parameters from the command line. Very, very bad idea. -1. If you want to do it correctly, tokenize the string you receive fin the third parameter of WinMain. And yes, there are plenty of Windows programs that interpret command line parameters. For example the "Printing" context menu in Windows Explorer starts a program with command line parameters. – datenwolf Sep 01 '11 at 16:35
  • datenwolf, They are still there using __argv and __argc macros (at least in MSVC) or GetCommandLine/CommandLineToArgv winapi function. – Cray Dec 03 '11 at 02:22
  • This solved the issue for me. Many thank :)) – Tayab Jun 04 '21 at 08:51
6

May be the better option will be not to simply remove (as Andy M suggested) but edit *.pro file adding something like

CONFIG(debug, debug|release) {
    CONFIG *= console
} 
else {
    CONFIG -= console
}

In debug you can see console window but not in release. I like it. =)

Wildcat
  • 8,701
  • 6
  • 42
  • 63
6

If you use Properties->Linker->System->SubSystem | Windows

And get a linker error.

You can look at Linker->Advanced-> Entry Point

and set the value to the name of your "main" function.

That is your Entry Point becomes, main, if your main function is a "main".

msc
  • 33,420
  • 29
  • 119
  • 214
Guest
  • 61
  • 1
  • 1
4

I would suggest to check the presence of the following line in your .PRO file :

CONFIG += console

If you can find it, remove it ! It should fix your issue !

Hope it helps !

Andy M
  • 5,945
  • 7
  • 51
  • 96
  • The actual Visual Studio project is created using the configuration parameters in the QMake project file, so this could very well be the cause. – Veeti Jan 26 '10 at 13:45
  • Erm, in the VCProj properties, maybe by going in Links Edition, System and finally Subsystem... Try putting the value "Windows (/SUBSYSTEM:WINDOWS)"... I'm not really sure you can do it like that tho... – Andy M Jan 26 '10 at 13:51
  • 1
    I quickly tested in one of my application and for some obscure reason, i need to add the following lib : c:\...\Qt\4.6.0-vs2008\lib\qtmain.lib in VCProj Properties-> Links Edition -> Entry -> Additionnal Dependencies – Andy M Jan 26 '10 at 14:01
  • 1
    And it looks like it's the final solution :) http://lists.trolltech.com/qt-interest/2005-12/thread00170-0.html – Andy M Jan 26 '10 at 14:04
2

For those of you editing the .vcxproj directly, you want to add a SubSystem with the value Windows to your Link ItemDefinitionGroup as follows:

<ItemDefinitionGroup>
  <Link>
    <SubSystem>Windows</SubSystem>
  </Link>
</ItemDefinitionGroup>
Dogmatixed
  • 794
  • 1
  • 11
  • 33
0

Go to: Projects --> Run and uncheck Run in terminal checkbox

Artem Zaytsev
  • 1,621
  • 20
  • 19
0

step 1:- Set Properties->Linker->System->SubSystem is "Windows (/SUBSYSTEM:WINDOWS)"
Step 2:- Linker->Advanced-> Entry Point "main"

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Milind Morey
  • 2,100
  • 19
  • 15
0

If all of the properties settings not working above, maybe check to delete 'return app.exec' and 'system("pause")' would help

-1

This worked for me:

CONFIG(debug, debug|release) {
    CONFIG *= console
} 
else {
    CONFIG -= console
}

I needed to run an exe to monitor a file using QFileSystemWatcher so I used this:

CONFIG -= console
Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
Festus Fashola
  • 151
  • 2
  • 9