19

I want to write one c++ program, compiling and linking .cpp gives .exe file. if i double click on that and execute it a console gets opened and closed. I don't want that console to appear at all. Please Help.

Jörg Beyer
  • 3,631
  • 21
  • 35
Lokesh Kumar S
  • 201
  • 1
  • 2
  • 4

7 Answers7

28

There are two ways for a Windows program to produce a console window:

  • The program is linked as a console subsystem exe, which is a request to Windows to always provide an associated console window.

  • The program's code itself creates a console window.

The first option, console subsystem, is by far most likely.

With the MinGW g++ compiler just add the option

-mwindows

With the Visual C++ compiler, if you're compiling from the command line, add the options

/link /subsystem:windows /entry:mainCRTStartup

If you're using Visual Studio, change the subsystem to windows and change the entry point to mainCRTStartup in the linker options.

With Microsoft's compiler it can be easier to just link with a module that contains a WinMain function that itself is a non-standard startup function, and that in violation of the C++ standard calls the ordinary standard main. That's because with GUI subsystem (subsystem "windows") Microsoft's compiler, as opposed to e.g. g++, does not by default recognize a standard main. It is simply a Microsoft thing (presumably it started as a vendor lock-in thing).

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • @Cheersandhth.-Alf: I agree, your answer *is* correct and complete. The only reason we've had this long thread is because of the "presumably it started as a vendor lock-in thing" comment. – Larry Osterman Mar 14 '12 at 15:42
  • It bit me once, so noting it here, `-mwindwos` is technically a linker option. – legends2k Sep 12 '18 at 14:38
  • 1
    @legends2k: Yes, it basically results in `-subsystem=windows` being passed to the linker. Which you can do via g++ option `-Wl,-subsystem=windows`. Or, while I did test that remembered option quickly before writing it, I see the linker's help doesn't specify an `=` sign. So probably more precisely it results in `-subsystem windows` being passed to the linker, which you can do via g++ option `-Wl,-subsystem,windows`. There. Hopefully. :) – Cheers and hth. - Alf Sep 12 '18 at 16:40
  • Changing the entry point isn't strictly necessary. [You can use `editbin` to change the `SUBSYSTEM` type of an existing executable](https://stackoverflow.com/a/2424210/) or explicitly call `link.exe /SUBSYSTEM:WINDOWS /ENTRY:main`. – jamesdlin Sep 15 '22 at 18:51
  • @jamesdlin: your example `link.exe /SUBSYSTEM:WINDOWS /ENTRY:main` is botched. For a hosted C++ program you need an entry point that /calls/ `main`, after e.g. performing dynamic initialization of namespace scope variables. With Microsoft's runtime `mainCRTStartup` is one example. – Cheers and hth. - Alf Sep 17 '22 at 09:06
5

If you want to create a console type program with a hidden console, then make this the first line of your main routine:

ShowWindow( GetConsoleWindow(), SW_HIDE );
ravenspoint
  • 19,093
  • 6
  • 57
  • 103
2

Weaker preconditions:

If your program does not have console child processes, simply FreeConsole(); should do it.

Else

a (hidden) window is required in order to not have console popups from child processes, i.e. as mentioned above

ShowWindow (GetConsoleWindow(), SW_HIDE);

Sam Ginrich
  • 661
  • 6
  • 7
2

It sounds like you need to update your Windows Visual Studio project settings to not be a console app.

If this isn't the case, then please post more information about your environment and tools.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • I dont want to work with visual c++. Cant i able to write a c++ program without VC++, which allows silent execution(I dont want console to appear.) – Lokesh Kumar S Mar 08 '12 at 14:15
  • Outside of Visual C++, I believe that [hmjd's answer](http://stackoverflow.com/a/9618927/25507) is correct. – Josh Kelley Mar 08 '12 at 14:18
1

On CODEBLOCKS, besides what @ravenspoint said, you have to put this line on your first line of code:

#define _WIN32_WINNT 0x0501 //this is for XP

And then:

ShowWindow (GetConsoleWindow(), SW_HIDE);
Felipe Volpato
  • 334
  • 1
  • 5
1

make sure you define _WINDOWS or WINDOW during compile and linking. (depending on your environment).

On the commandline you can do this as follows

cl -D_WINDOWS  program.cpp
0

If you can't move to a Window application (I mean with GUI) because sometimes you may need to use the console for output you can use following code to hide the console window:

HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • no here also user can able to see the console coming and going. – Lokesh Kumar S Mar 08 '12 at 14:12
  • How you run that application? From another application? By user double click? – Adriano Repetti Mar 08 '12 at 14:17
  • Run the "console" application with CreateProcess, set STARTF_USESHOWWINDOW flag in the STARTUPINFO structure and SW_HIDE for wShowWindow. (Anyway do you really have to create a _console_ application if after all you do not want to show the console?) – Adriano Repetti Mar 08 '12 at 14:27
  • [quote](Anyway do you really have to create a console application if after all you do not want to show the console?)[\quote] – Lokesh Kumar S Mar 09 '12 at 06:26