0

我刚刚开始使用 MinGW,但遇到了一个问题,即它没有输出可执行文件或目标文件,或者其他任何东西。修复了一些错误后,一切编译正常,但没有输出可执行文件。当我第一次安装 MinGW 时,我在一个简单的 hello world 程序上对其进行了测试,一切正常,但我正在尝试编写一个基本的 Windows 应用程序,但它不工作。我以前使用过 gcc,但只是简单地说,我对此一无所知。

C:\Users\Cole\Dev\Hello Windows\>gcc win_main.c -o win_main

这是 win_main.c 文件:

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

/*
 * Global Variables
 */
HWND g_hwnd = NULL;
HINSTANCE g_hinst = NULL;

/*
 * Forward Declarations
 */
LRESULT CALLBACK win_proc(HWND h_wnd, UINT message, WPARAM w_param, LPARAM l_param);
HRESULT init_window(HINSTANCE h_instance, int cmd_show);

/*
 * Main entry point to the application.
 */
int WINAPI WinMain(HINSTANCE h_instance, HINSTANCE h_previnstance, LPSTR cmd_line, int cmd_show) {

    if(FAILED(init_window(h_instance, cmd_show)))
        return -1;

    MSG msg = {0};
    while(GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

/*
 * Register window class and create the window.
 */
HRESULT init_window(HINSTANCE h_instance, int cmd_show) {

    /* Register window class. */
    WNDCLASSEX wcx;
    wcx.cbSize = sizeof(WNDCLASSEX);
    wcx.style = CS_VREDRAW | CS_HREDRAW;
    wcx.lpfnWndProc = win_proc;
    wcx.cbClsExtra = 0;
    wcx.cbWndExtra = 0;
    wcx.hInstance = h_instance;
    wcx.hIcon = NULL;
    wcx.hIconSm = NULL;
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcx.lpszMenuName = NULL;
    wcx.lpszClassName = "BasicWindowClass";

    if(!RegisterClassEx(&wcx)) {
        printf("Failed to register window class.\n");
        return E_FAIL;
    }

    /* Create the window. */
    g_hinst = h_instance;
    RECT rc = {0, 0, 640, 480};
    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
    g_hwnd = CreateWindow("BasicWindowClass", "Windows Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
        rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, g_hinst, NULL);

    if(g_hwnd == NULL) {
        printf("Failed to create the window.\n");
        return E_FAIL;
    }

    ShowWindow(g_hwnd, cmd_show);

    return S_OK;
}

LRESULT CALLBACK win_proc(HWND h_wnd, UINT message, WPARAM w_param, LPARAM l_param) {

    PAINTSTRUCT ps;
    HDC hdc;

    switch(message) {
        case WM_PAINT:
            hdc = BeginPaint(h_wnd, &ps);
            EndPaint(h_wnd, &ps);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(h_wnd, message, w_param, l_param);
    }

    return 0;
}
4

1 回答 1

1

您应该添加 -mwindows gcc -mwindows win_main.c -o win_main。我猜您的第一个程序使用“主”函数作为入口点...

于 2012-10-26T21:33:56.887 回答