Thread: [Linker error]

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102

    [Linker error]

    http://img149.imageshack.us/img149/8595/errorpv4.png
    ^that happens when i compile this^
    Code:
    #include <windows.h>
    
    const char g_szClassName[] = "myWindowClass";
    char szFileName[MAX_PATH]="";;
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            case WM_LBUTTONDOWN:
    
        OPENFILENAME ofn;
        ZeroMemory(&ofn, sizeof(ofn));
        ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
        ofn.hwndOwner = hwnd;
        ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
        ofn.lpstrFile = szFileName;
        ofn.nMaxFile = MAX_PATH;
        ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
        ofn.lpstrDefExt = "txt";
    
        if(GetOpenFileName(&ofn))
        {
            // Do something usefull with the filename stored in szFileName 
        }
                 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;
    
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    I'm using Dev-Cpp 4.9.9.2 and have no idea how to fix this.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I have the same compiler and your program compiles and links without error. To create the project, I selected menu items File --> New --> Project --> Windows Application, entered the project name, and hit the Ok button. The compiler generated a starter shell program, I deleted everything in the c++ file and added the code that you posted.

  3. #3
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    make sure you link with comdlg32.lib

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Thanks Dragon! That worked! I'll always select new project, rather than just source file for windows programs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Linker error] undefined reference to `GetStockObject@4'
    By NecroFromHell in forum Windows Programming
    Replies: 7
    Last Post: 10-07-2010, 04:27 AM
  2. DevC++ no longer compiling, [Linker error]
    By Tropod in forum C++ Programming
    Replies: 14
    Last Post: 12-26-2008, 03:11 PM
  3. [Linker error] undefined reference.(GTK, devcpp)
    By Firestarter in forum C Programming
    Replies: 2
    Last Post: 05-05-2006, 11:04 AM
  4. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  5. OpenGL in devc++
    By Mipix in forum Game Programming
    Replies: 5
    Last Post: 07-21-2003, 01:21 PM