MinGW, creating a GUI application with a console

I use MinGW to build my application on Windows. When compiling and linking, the -mwindows option is placed on the command line to have Win32 API functions.

More specifically: when invoking GCC from MinGW without "-mwindows", for example:

c:\>g++ -c main.cpp c:\>g++ -o main.exe main.o 

"main.exe" after the 2 above commands will work with the console, and the Win32 API functions will not be used.

When invoking GCC from MinGW with "-mwindows":

 c:\>g++ -c main.cpp c:\>g++ -o main.exe main.o -mwindows 

Now, referring to '-mwindows', 'main.exe' can use the Win32 API, however, it does not start the console when the application starts.

This "-windows" option disables the console, which prevents the printing of debugging information. Any way to save both the console and the '-mwindows' option ?

+4
source share
4 answers

I have no evidence for this answer, only a few experiments that were successful. If I have a welcome application, for example:

 #include <stdio.h> #include <windows.h> int main(void) { puts("hi"); MessageBox(NULL, "test", "test", NULL); GetStockObject(0); return 0; } 

I cannot compile it using -mconsole because the linker complains about GetStockObject . But when I add the necessary library with -lgdi32 switch to my command line, the application compiles and runs cleanly. Perhaps this is a way to keep the console and gdi. This is the command line:

 gcc -mconsole test_gdi.c -lgdi32 
+6
source

The -mconsole switch -mconsole used to indicate that you want to target the console subsystem. You really want to do this to make sure your process connects to an existing console if it is running from a console application. For example, suppose you omit the targeting route for the GUI subsystem and then AllocConsole() according to your own answer. Then you will find that your application displays a new console, and does not use the existing one when launched from another console application, for example. cmd.exe .

If you need to use other libraries, you can add them on the command line using -l . There is nothing special in the console application, which means that it cannot refer to any Win32 API function. It's just that the default library set associated with -mconsole skips some of the libraries you -mconsole .

On the other hand, you can use both -mconsole and -mwindows to create your application. They are not mutually exclusive.

 gcc -mconsole -mwindows main.c 

This creates an application designed for the console subsystem. And you automatically link the standard -mwindows Win32 library set. This is probably the easiest way to achieve your goal.

+15
source

I have found the answer. As taken from Using STDIN with AllocConsole ()

 AllocConsole(); freopen("CONIN$", "r",stdin); freopen("CONOUT$","w",stdout); freopen("CONOUT$","w",stderr); 

It works like magic!

Link for 'freopen': http://www.cplusplus.com/reference/clibrary/cstdio/freopen/

+1
source

You need to manually capture hInstance and nCmdShow (WinMain arguments). You can use the following C functions for this:

 HINSTANCE GetHInstance( ) { return (HINSTANCE) GetModuleHandleW(NULL); } int GetNCmdShow() { STARTUPINFOW startupInfo; GetStartupInfoW(&startupInfo); if ((startupInfo.dwFlags & STARTF_USESHOWWINDOW) != 0) { return startupInfo.wShowWindow; } return SW_SHOWDEFAULT; 

}

0
source

All Articles