本文介绍了MinGW,使用控制台构建 GUI 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MinGW 在 Windows 上构建我的应用程序.编译链接时在命令行中加入-mwindows"选项,实现Win32 API函数.

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.

更具体地说:在没有-mwindows"的情况下调用 MinGW 的 GCC 时:

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

上面2个命令行后面的'main.exe'会用控制台运行,Win32 API函数将无法使用.

The 'main.exe' after the 2 command lines above will run with a console, and Win32 API functions won't be usable.

当像这样使用-mwindows"调用MinGW的GCC时:

When calling GCC of MinGW with "-mwindows" like this:

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

现在与-mwindows"链接,main.exe"可以使用 Win32 API,但是,当应用程序运行时它不会启动控制台.

Now linking with '-mwindows', the 'main.exe' can use Win32 API, however, it doesn't start a console when the application runs.

这个-mwindows"选项禁用控制台,这使我无法打印出调试信息.有什么方法可以同时保留控制台和-mwindows"选项?

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'?

推荐答案

我没有这个答案的证据,只有一些成功的实验.如果我有一个 hello 应用程序,像这样:

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;
}

我不能-mconsole 编译它,因为链接器抱怨 GetStockObject.但是,当我在命令行上使用 -lgdi32 开关 添加必要的库时,应用程序编译并干净地执行.也许这是同时保留控制台和 gdi 的方式.这是命令行:

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

这篇关于MinGW,使用控制台构建 GUI 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 21:27