MinGW不输出可执行文件或目标文件

时间:2012-10-26 21:21:34

标签: c mingw

我刚刚开始使用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;
}

1 个答案:

答案 0 :(得分:1)

你应该添加-mwindows gcc -mwindows win_main.c -o win_main。我猜你的第一个程序是使用'main'函数作为入口点......