MinGW, build GUI application with console

console, debugging, linker, mingw, windows

Solution

I have no evidence for this answer, only a bit of experiments that were successful. If I have a hello app, like this:

#include <stdio.h>
#include <windows.h>

int main(void)
{
    puts("hi");
    MessageBox(NULL, "test", "test", NULL);
    GetStockObject(0);
    return 0;
}

I cannot compile it with `-mconsole`, because linker complains about `GetStockObject`. But when I add the necessary library with `-lgdi32` switch on my command line, the app compiles and executes cleanly. Maybe this is the way to keep both console and gdi. This is the command line:

gcc -mconsole test_gdi.c -lgdi32

Problem

I'm using MinGW to build my application on Windows. When compiling and linking, the option "-mwindows" is put in command line to have Win32 API functions. To be more specific: when calling GCC of MinGW without "-mwindows" like this: ``` c:\>g++ -c main.cpp c:\>g++ -o main.exe main.o ``` The 'main.exe' after the 2 command lines above will run with a console, and Win32 API functions won't be usable. When calling GCC of MinGW with "-mwindows" like this: ``` c:\>g++ -c main.cpp c:\>g++ -o main.exe main.o -mwindows ``` Now linking with '-mwindows', the 'main.exe' can use Win32 API, however, it doesn't start a console when the application runs. This "-mwindows" option disables the console, which makes me not able to print out debugging info. Any way to keep both console and the option '-mwindows'?

Original source

Related problems