Forum: Linker fel?

Forum huvudsida -> Programmering -> Linker fel?

Sidor: 1

Till botten

GalenBanan 14:33 - 15:e Maj 2006 | Post #1
Medlem
Inlägg: 10


Skicka PM
VC++ påstår att jag har ett linker fel eller något liknande, detta står i loggen:

------ Build started: Project: Lost Island(Project Name), Configuration: Debug Win32 ------
Linking...
main.obj : error LNK2019: unresolved external symbol "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) referenced in function "int __cdecl InitWindow(char *,int,int,int)" (?InitWindow@@YAHPADHHH@Z)
D:\Documents and Settings\André Nyström\My Documents\Lost Island(Project Name)\VC++\Lost Island(Project Name)\Debug\Lost Island(Project Name).exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://d:\Documents and Settings\André Nyström\My Documents\Lost Island(Project Name)\VC++\Lost Island(Project Name)\Debug\BuildLog.htm"
Lost Island(Project Name) - 2 error(s), 0 warning(s)

======= Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =======


Felet med WndProc har jag haft förrut också, alltid på samma ställa, nämligen här:

    hInstance            = GetModuleHandle(NULL);
    WC.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    WC.lpfnWndProc        = (WNDPROC)WndProc;
    WC.cbClsExtra        = 0;
    WC.cbWndExtra        = 0;
    WC.hInstance        = hInstance;
    WC.hIcon            = LoadIcon(NULL, IDI_WINLOGO);
    WC.hCursor            = LoadCursor(NULL, IDC_ARROW);
    WC.hbrBackground    = NULL;
    WC.lpszMenuName        = NULL;
    WC.lpszClassName    = WNDCLSNAME;

Vet nån vad som är fel?

-------------------------
C++!!!



Nissebosselasse 14:48 - 15:e Maj 2006 | Post #2
Medlem
Inlägg: 490


Skicka PM
Posta hela koden, tack Smiley

Det känns som om du använder funktionen innan du har deklarerat den (mha en funktionsprototyp).

-------------------------
http://pushingcows.se
Gula Nallen

Senast redigerad 14:48 - 15:e Maj 2006


GalenBanan 14:55 - 15:e Maj 2006 | Post #3
Medlem
Inlägg: 10


Skicka PM

#ifndef __INITWINANDGL_H__
#define __INITWINANDGL_H__

// includes
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "Vars.h"

// defines
#define WNDCLSNAME "LI"

// function define
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

// funtions
GLvoid ReSizeGLScene(GLsizei Width, GLsizei Height)
{
    if(Height==0)
    {
        Height = 1;
    }

    glViewport(0,0,Width,Height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f, (GLfloat)Width/(GLfloat)Height,0.1f, 1000.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int InitGL(GLvoid)
{
    glShadeModel(GL_SMOOTH);

    glClearColor(0.0f,0.0f,0.0f,0.0f);

    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    return TRUE;
}

GLvoid KillGL(GLvoid)
{
    ChangeDisplaySettings(NULL, 0);
    ShowCursor(true);

    if(hRC)
    {
        if(!wglMakeCurrent(NULL, NULL))
        {
            MessageBox(NULL, "Couldn't release the RC and DC!", "SHUTDOWN ERROR!", MB_OK|MB_ICONINFORMATION);
        }

        if(!wglDeleteContext(hRC))
        {
            MessageBox(NULL, "Couldn't release the rendering context!", "SHUTDOWN ERROR!", MB_OK|MB_ICONINFORMATION);
        }
        hRC=NULL;
    }

    if(hDC && !ReleaseDC(hWnd, hDC))
    {
        MessageBox(NULL, "Couldn't release the Device Context!", "SHUTDOWN ERROR!", MB_OK|MB_ICONINFORMATION);
        hDC = NULL;
    }

    if(hWnd && !DestroyWindow(hWnd))
    {
        MessageBox(NULL, "Couldn't destroy the hWnd!", "SHUTDOWN ERROR!", MB_OK|MB_ICONINFORMATION);
        hWnd=NULL;
    }

    if(!UnregisterClass(WNDCLSNAME, hInstance))
    {
        MessageBox(NULL, "Couldn't unregister class!", "SHUTDOWN ERROR!", MB_OK | MB_ICONINFORMATION);
        hInstance=NULL;
    }
}

BOOL InitWindow(char *Title, int Width, int Height, int Bits)
{
    GLuint        PixelFormat;

    WNDCLASS    WC;

    RECT    WindowRect;
    WindowRect.left=(long)0;
    WindowRect.right=(long)Width;
    WindowRect.top=(long)0;
    WindowRect.bottom=(long)Height;



    hInstance            = GetModuleHandle(NULL);
    WC.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    WC.lpfnWndProc        = (WNDPROC)WndProc;
    WC.cbClsExtra        = 0;
    WC.cbWndExtra        = 0;
    WC.hInstance        = hInstance;
    WC.hIcon            = LoadIcon(NULL, IDI_WINLOGO);
    WC.hCursor            = LoadCursor(NULL, IDC_ARROW);
    WC.hbrBackground    = NULL;
    WC.lpszMenuName        = NULL;
    WC.lpszClassName    = WNDCLSNAME;

    if(!RegisterClass(&WC))
    {
        MessageBox(NULL, "Couldn't register WC class!", "", MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    DEVMODE dmScreenSettings;
    memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
    dmScreenSettings.dmSize = sizeof(dmScreenSettings);
    dmScreenSettings.dmPelsWidth = Width;
    dmScreenSettings.dmPelsHeight = Height;
    dmScreenSettings.dmBitsPerPel = Bits;
    dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

    if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN)!= DISP_CHANGE_SUCCESSFUL)
    {
        MessageBox(NULL, "Fullscreen is not supported by your video card", "ERROR!", MB_OK|MB_ICONINFORMATION);
        return FALSE;
    }

    AdjustWindowRectEx(&WindowRect, WS_POPUP, FALSE, WS_EX_APPWINDOW);

    if(!(hWnd=CreateWindowEx(WS_EX_APPWINDOW,
                             WNDCLSNAME,
                             Title,
                             WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP,
                             0,
                             0,
                             WindowRect.right-WindowRect.left,
                             WindowRect.bottom-WindowRect.top,
                             NULL,
                             NULL,
                             hInstance,
                             NULL)))
    {
        KillGL();
        MessageBox(NULL, "Couldn't Create Window!", "ERROR!", MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    static PIXELFORMATDESCRIPTOR pfd=
    {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW |
        PFD_SUPPORT_OPENGL |
        PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        Bits,
        0,0,0,0,0,0,
        0,
        0,
        0,
        0,0,0,0,
        16,
        0,
        0,
        PFD_MAIN_PLANE,
        0,
        0,0,0
    };

    if(!(hDC=GetDC(hWnd)))
    {
        KillGL();
        MessageBox(NULL, "Couldn't Create The DC", "ERROR!", MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    if(!(PixelFormat=ChoosePixelFormat(hDC, &pfd)))
    {
        KillGL();
        MessageBox(NULL, "Couldn't Choose PixelFormat", "ERROR!", MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    if(!(SetPixelFormat(hDC, PixelFormat, &pfd)))
    {
        KillGL();
        MessageBox(NULL, "Couldn't Set PixelFormat", "ERROR!", MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    if(!wglCreateContext(hDC))
    {
        KillGL();
        MessageBox(NULL, "Couldn't Create The A GL RC", "ERROR!", MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    if(!wglMakeCurrent(hDC, hRC))
    {
        KillGL();
        MessageBox(NULL, "Couldn't Activate GL RC", "ERROR!", MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    ShowWindow(hWnd, SW_SHOW);
    SetForegroundWindow(hWnd);
    SetFocus(hWnd);
    ReSizeGLScene(Width, Height);

    if(!InitGL())
    {
        KillGL();
        MessageBox(NULL, "Couldn't Init GL!", "ERROR!", MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    return TRUE;
}

#endif


main.cpp kanske man ska visa också:


// includes
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "InitWinAndGL.h"
#include "DrawScene.h"
#include "Vars.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    MSG Msg;
    BOOL Done=FALSE;

    if(!InitWindow("LI" ,800, 600, 16))return 0;

    while(!Done)
    {
        if(PeekMessage(&Msg, NULL, 0,0, PM_REMOVE))
        {
            if(Msg.message==WM_QUIT)
            {
                Done=TRUE;
            }
            else
            {
                TranslateMessage(&Msg);
                DispatchMessage(&Msg);
            }
        }
        else
        {
            if(Active)
            {
                DrawScene();
                SwapBuffers(hDC);
            }
        }
    }
    KillGL();
    return (Msg.wParam);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, LPARAM lParam, WPARAM wParam)
{
    switch(uMsg)
    {
    case WM_ACTIVATE:
        {
            if(!HIWORD(wParam))
            {
                Active = TRUE;
            }
            else
            {
                Active = FALSE;
            }
            return 0;

        }

    case WM_SYSCOMMAND:
        {
            switch(wParam)
            {
            case SC_SCREENSAVE:
            case SC_MONITORPOWER:
                return 0;
            }
            break;
        }

    case WM_CLOSE:
        {
            PostQuitMessage(0);
            return 0;
        }

    case WM_KEYDOWN:
        {
            Keys[wParam] = TRUE;
            return 0;
        }

    case WM_KEYUP:
        {
            Keys[wParam] = FALSE;
            return 0;
        }

    case WM_SIZE:
        {
            ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));
            return 0;
        }
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


-------------------------
C++!!!



Nissebosselasse 15:08 - 15:e Maj 2006 | Post #4
Medlem
Inlägg: 490


Skicka PM
Vad händer om du tar bort type-castning (WNDPROC) framför tilldelningen (WC.lpfnWndProc = ...), så att det blir:
  1. hInstance = GetModuleHandle(NULL);
  2. WC.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  3. WC.lpfnWndProc = WndProc;
  4. WC.cbClsExtra = 0;
  5. WC.cbWndExtra = 0;
  6. WC.hInstance = hInstance;
  7. WC.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  8. WC.hCursor = LoadCursor(NULL, IDC_ARROW);
  9. WC.hbrBackground = NULL;
  10. WC.lpszMenuName = NULL;
  11. WC.lpszClassName = WNDCLSNAME;


Rad 4 alltså.

------

En annan sak: varför använder du PeekMessage i huvudloopen? Den tar ju inte bort meddelandet från kön, utan "smygtittar" bara. Borde kanske byta till GetMessage (i main.cpp)?

Nej, jag läste på lite mer nu (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/peekmessage.asp). Strunta i det sista.

-------------------------
http://pushingcows.se
Gula Nallen

Senast redigerad 15:17 - 15:e Maj 2006


GalenBanan 15:47 - 15:e Maj 2006 | Post #5
Medlem
Inlägg: 10


Skicka PM
------ Build started: Project: Lost Island(Project Name), Configuration: Debug Win32 ------
Compiling...
main.cpp
Linking...
main.obj : error LNK2019: unresolved external symbol "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) referenced in function "int __cdecl InitWindow(char *,int,int,int)" (?InitWindow@@YAHPADHHH@Z)
D:\Documents and Settings\André Nyström\My Documents\Lost Island(Project Name)\VC++\Lost Island(Project Name)\Debug\Lost Island(Project Name).exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://d:\Documents and Settings\André Nyström\My Documents\Lost Island(Project Name)\VC++\Lost Island(Project Name)\Debug\BuildLog.htm"
Lost Island(Project Name) - 2 error(s), 0 warning(s)

======= Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =======



Det händer när jag tar bort (WNDPROC), dvs inte så mycket...

-------------------------
C++!!!



Nissebosselasse 18:50 - 15:e Maj 2006 | Post #6
Medlem
Inlägg: 490


Skicka PM
Flytta över WndProc i den andra filen, precis på raden där det står

// function define
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

...och ta bort den här funktionsprototypen?

-------------------------
http://pushingcows.se
Gula Nallen



GalenBanan 19:54 - 15:e Maj 2006 | Post #7
Medlem
Inlägg: 10


Skicka PM
Funkade inget vidare :/

-------------------------
C++!!!



Nissebosselasse 14:42 - 16:e Maj 2006 | Post #8
Medlem
Inlägg: 490


Skicka PM
Skriv om allting, och lägg alla funktionsprototyper i en fil.

-------------------------
http://pushingcows.se
Gula Nallen



Sotai 14:48 - 24:e Maj 2006 | Post #9
Medlem
Inlägg: 5


Skicka PM
Jag tror att du lyckats mixtra bort något systemlib. Gör ett helt nytt projekt och importera kodfilerna och se ifall det funkar. Du har ju tex lyckas ta bort $ framför (Project Name), så du har ändrat endel i projektinställningarna.

-------------------------
Ingen signatur!



Sidor: 1

Forum huvudsida -> Programmering -> Linker fel?
Atom feed

Du får inte posta i den här tråden | Till toppen