Cole Rowland
Cole Rowland

Reputation: 149

MinGW isn't outputting executable or object file

I've just started using MinGW and I'm having an issue where it isn't outputting an executable or an object file, or anything really. After fixing a few errors everything compiles fine, but no executable is being outputted. When I first installed MinGW I tested it on a simple hello world program and everything worked correctly, but I'm trying to write a basic windows application and it's not working. I've worked with gcc before, but only briefly, I don't really know anything about it.

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

Here's the win_main.c file:

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

Upvotes: 0

Views: 250

Answers (1)

user18428
user18428

Reputation: 1201

You should add -mwindows gcc -mwindows win_main.c -o win_main .I guess your first program was using a 'main' function as entry point...

Upvotes: 1

Related Questions