Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I dont want console to appear when i run c++ program

Tags:

c++

windows

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.

like image 625
Lokesh Kumar S Avatar asked Mar 08 '12 14:03

Lokesh Kumar S


People also ask

How do you hide a console window?

"SW_HIDE" hides the window, while "SW_SHOW" shows the window.

What is Mwindows?

-mwindows. This option is available for Cygwin and MinGW targets. It specifies that a GUI application is to be generated by instructing the linker to set the PE header subsystem type appropriately.


2 Answers

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

like image 87
Cheers and hth. - Alf Avatar answered Oct 12 '22 23:10

Cheers and hth. - Alf


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 );
like image 34
ravenspoint Avatar answered Oct 12 '22 23:10

ravenspoint