Nigh7Sh4de
Nigh7Sh4de

Reputation: 455

C++ cannot create object file because requires resources

Okay so I have a C++ Win32 program that should have a custom icon. I have created a resource.h file and a resource.rc file (which I have compiled into a resource.o file). Now I need to compile my .cpp file and link my source object file and my resource object file into a final executable. However my cpp does not see the resources that are in the resource file. What I mean is during the compilation stage (g++ -c mine.cpp) it doesn't know what my custom icon is.

Here is my resource.h file:

    #define IDR_MYMENU 101
    #define IDI_MYICON 201

    #define ID_FILE_EXIT 9001
    #define ID_STUFF_GO 9002

Here is my resource.rc file:

    #include "a.h"

    IDR_MYMENU MENU
    BEGIN
        POPUP "&File"
        BEGIN
            MENUITEM "E&xit", ID_FILE_EXIT
        END

        POPUP "&Stuff"
        BEGIN
            MENUITEM "&Go", ID_STUFF_GO
            MENUITEM "G&o somewhere else", 0, GRAYED
        END
    END

    IDI_STAR ICON "star.ico"

Finally here is the .cpp file:

#include <windows.h>

const char g_szClassName[] = "myWindowClass";

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_LBUTTONDOWN:
            MessageBox(NULL, "You pressed the left mouse button! :O", "MousePress", MB_OK | MB_ICONINFORMATION);
            break;      
        case WM_RBUTTONDOWN:
            MessageBox(NULL, "You pressed the right mouse button! :O", "MousePress", MB_OK | MB_ICONINFORMATION);
            break;
        case WM_MBUTTONDOWN:
            {
                char fileName[MAX_PATH];
                HINSTANCE hInstance = GetModuleHandle(NULL);

                GetModuleFileName(hInstance, fileName, MAX_PATH);
                MessageBox(hwnd, fileName, "The name of the program is:", MB_OK | MB_ICONINFORMATION);
            }
            break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize           = sizeof(WNDCLASSEX);
    wc.style            = 0;
    wc.lpfnWndProc      = WndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hInstance        = hInstance;
    wc.hIcon            = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_STAR));
    wc.hCursor          = LoadCursor(NULL, IDC_CROSS);
    wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName     = MAKEINTRESOURCE(IDR_MYMENU);
    wc.lpszClassName    = g_szClassName;
    wc.hIconSm          = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_STAR), IMAGE_ICON, 16, 16, 0);

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Registration Failed!", NULL, MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_DLGMODALFRAME,
        g_szClassName,
        "This is a window!",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 450,
        NULL, NULL, hInstance, NULL     
    );

    if (hwnd == NULL) {
        MessageBox(NULL, "HWND failed!", NULL, MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&Msg, NULL, 0, 0) > 0) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

Upvotes: 0

Views: 380

Answers (1)

Christian Rau
Christian Rau

Reputation: 45968

I think you need to include your resource.h in the .cpp file, as it defines your resource identifiers.

Upvotes: 1

Related Questions